1

My C++ code compiles and runs, but no output is printed to the console. I think it has something to do with the string variables, but im not sure. Im a total noob, any help would be appreciated. I am using codeblocks with GNU GCC Compiler.

#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;
};
3
  • Did you try to add endl in order to see some results ? Commented Jul 20, 2015 at 8:06
  • Hmm. Tried adding a new line to your strings? like doing ' << endl '. Depending on your environment, strings without newline might have strange effects ... Commented Jul 20, 2015 at 8:07
  • @alifirat is right, you should flush the stream (or any stream for that matter) before the program ends. Commented Jul 20, 2015 at 8:07

2 Answers 2

5

It must be >= instead of <=, otherwise your loops are not entered:

for(int bottles = 99; bottles >= 0; --bottles)
{
  cout << bottles << botlong << bottles << botshort;
  for(int lostB = 98; lostB >= 0; --lostB)
  {
    cout << secondline << lostB << botlong;
  }
}
Sign up to request clarification or add additional context in comments.

1 Comment

That was it. I appreciate the help. I guess with the way I had it, the expression (bottles <= 0) was never true, so it never entered the loop. Thanks a bunch for helping me understand this!
0

I think your condition in for loop doesn't hold on start. You have a typo, correct bottles<=0 to bottles>=0 and same with next for loop.

1 Comment

That was it, thanks! You and the above response helped me out a lot!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.