编译代码,但没有控制台输出

Code compiles, but no console output

本文关键字:控制台 输出 代码 编译      更新时间:2023-10-16

我的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;
  }
}

我认为你的条件在for循环不坚持启动。您有一个错字,将bottles<=0更正为bottles>=0,并与下一个for循环相同。