【问题标题】:Code compiles, but no console output代码编译,但没有控制台输出
【发布时间】:2015-07-20 08:04:37
【问题描述】:

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

【问题讨论】:

  • 您是否尝试添加endl 以查看一些结果?
  • 嗯。尝试在字符串中添加新行?喜欢做'
  • @alifirat 是对的,您应该在程序结束前flush 流(或任何流)。

标签: c++ gcc codeblocks gnu


【解决方案1】:

必须是&gt;=而不是&lt;=,否则你的循环不会进入:

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

【讨论】:

  • 就是这样。我很感激帮助。我想按照我的方式,表达式 (bottles
【解决方案2】:

我认为您在for 循环中的条件在开始时不成立。您有一个错字,将bottles&lt;=0 更正为bottles&gt;=0 并与下一个for 循环相同。

【讨论】:

  • 就是这样,谢谢!你和上面的回复对我帮助很大!
猜你喜欢
  • 2014-07-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-20
  • 2014-09-14
  • 2021-12-24
  • 1970-01-01
相关资源
最近更新 更多