When I "cout" below, it starts from 75. However, if I take out the last line
" cout << bottlesLeft <<" bottles of beer on the wall."<<endl;". It correctly prints out starting from 99. I have no clue.
1 2 3 4 5 6 7 8 9 10 11 12 13
int main ()
{
int bottlesLeft=99;
while (bottlesLeft>0)
{
cout << bottlesLeft <<" bottles of beer on the wall,"<<endl
<< bottlesLeft <<" bottles of beer,"<<endl
<< "Take one down, pass it around,"<<endl;
bottlesLeft = bottlesLeft-1;
cout << bottlesLeft <<" bottles of beer on the wall."<<endl;
}
}
I do not see any problem. With the statement you pointed out or without it the loop will be executed 99 times and its output will look like
99 bottles of beer on the wall
99 bottles of beer
Take one down, pass it around,
98 bottles of beer on the wall
98 bottles of beer on the wall
98 bottles of beer
Take one down, pass it around,
97 bottles of beer on the wall
and so on.
I definitely agreed with vlad from moscow, but I have two suggestions that may help you out.
1) Regarding Pebble's statement on "too many endl's", I would keep the endl's because it fits the format of the song. Although, I think you should "beautify" your code better as good programming practice:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
int main ()
{
int bottlesLeft=99;
while (bottlesLeft>0)
{
cout << bottlesLeft <<" bottles of beer on the wall," << endl;
// stop there and begin a new output
cout << bottlesLeft <<" bottles of beer,"<<endl;
cout << "Take one down, pass it around,"<<endl;
bottlesLeft = bottlesLeft-1;
cout << bottlesLeft <<" bottles of beer on the wall."<<endl;
}
//dont forget the return statement
return 0;
}
2) I also have a second suggestion to make this program easier to code, but also just as a trick to keep in mind. You could use the decrementation operator.
1 2 3 4 5
// changing line 9 by using the decrementation operator
bottlesLeft -= 1;
// or simply:
// --bottlesLeft;
Thanks for few suggestions. However with Microsoft Visual C++ 2010 Express, I am getting results starting with 75 bottles.... when I take the last line out "cout << bottlesLeft <<" bottles of beer on the wall.\n";" It is back to starting with 99 bottles.... So I am wondering if I shouldn't put anything after "bottlesLeft = bottlesLeft-1;". Any idea why it should be?
The problem is that your 4*99 lines cannot fit onto the screen. Therefore, the 99-75 bottles part gets lost somewhere up there, and you can only see the 74-0 bottles part. If you were to add cin.ignore(); // waits for a user response: (\n) key after your last line in the while loop, you would see that it does start from 99.
To fix this problem, you can set the height of the console to a bigger number. You can do this by adding this code:
1 2 3 4 5 6 7 8
#include <windows.h>
void InitConsole(int height, int width)
{
HANDLE wHnd; // gets needed console handle
wHnd = GetStdHandle(STD_OUTPUT_HANDLE); // gets needed console handle
COORD bufferSize = {width, height}; // the buffer for the screen
SetConsoleScreenBufferSize(wHnd, bufferSize); // sets the screen's buffer
}