No clue

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;

	} 
}
Last edited on
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.
Last edited on
Taking out any one of the endl's fix's the problem, No idea why your getting the
display starting from 75 though.
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?
I have seen mine() with many more <<endl; but they worked fine. So why it should cause a problem here?
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
}

and running the InitConsole function.


Here is what your final code should look like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
using namespace std;

#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
}

int main ()
{   
	InitConsole(450,80);
	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;
		cout << bottlesLeft <<" bottles of beer on the wall."<<endl;
             // cin.ignore();
	} 
      cin.ignore();
}


-Usandfriends

Remember to mark as "Solved".
Last edited on
Thank you very much, Usandfriends.
Topic archived. No new replies allowed.