我的C++代码可以编译并运行,但是没有输出输出到控制台。我认为这与字符串变量有关,但我不确定。我是一个新手,将不胜感激。我在GNU GCC编译器中使用代码块。

#include <iostream>
#include <string>
using namespace std;

int main()
{
    string botlong, botshort, secondline;
    botlong = "bottles of beer on the wall,";
    botshort = "bottles of beer";
    secondline = "Take one down and pass it around,";
    for(int bottles = 99; bottles<=0; bottles--)
    {
        cout<<bottles <<botlong <<bottles <<botshort;
        for(int lostB = 98; lostB<=0; lostB--)
        {
            cout<<secondline<<lostB<<botlong;
        }
    }
    return 0;
};

最佳答案

它必须是>=而不是<=,否则不会输入循环:

for(int bottles = 99; bottles >= 0; --bottles)
{
  cout << bottles << botlong << bottles << botshort;
  for(int lostB = 98; lostB >= 0; --lostB)
  {
    cout << secondline << lostB << botlong;
  }
}

关于c++ - 代码编译,但没有控制台输出,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31511491/

10-11 16:18