PROGRAMMING ISSUE USING THE WHILE LOOP

I am unable to get the following program to run properly. It is supposed to start at 99 and count down to 0, however no matter what number I use for int x, it will only start at 59, and count down from that. What am I doing wrong?

Thank you...

Terry

HERE IS THE CODE

#include <iostream>

using namespace std;

int main()
{
int x = 99;
while ( x > 0 )
{
cout << x << " bottles of beer on the wall, \n";
cout << x << " bottles of beer, \n";
cout << "If one of those bottles of beer should fall, \n";
x = x-1;
cout << x << " bottles of beer on the wall, \n";
cout << '\n';
}
}
Last edited on
Your program is functioning just fine, the problem is the limitation of the console.

Try changing the while condition to x > 75 and you will indeed see the number starts at 99

Right.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

#include <iostream>
using namespace std;

int main()
{
	int x = 99; 
	
	while (x > 0) 
	{
		cout << x << "Bottles of beer on the wall,\n";
		cout << x << "Bottles of ber, \n";
		cout << "If one of those bottles of beer should fall, \n";
		x--;
		cout << x << " bottles of beer on the wall.\n\n";
	}
	return 0;
}


*sigh* Post_count += 1;
George & BHX...

Thank you for your feedback. I've tried both of your suggestions, and still the program will not run as it should.

George - When I tried your suggestion, it indeed will start at 99, however will only count down to the number that condition 'x' is set to. eg - If I set it to 40, it will count down to 40 (with the beginning number being 99), BUT... as soon as I change condition 'x' to any number lower than 40, it removes one number from the starting count for every number below 40. Again, another example - if I set condition 'x' to 39, it starts counting down from 98.

BHX - I changed the code to what you provided, but I still can't get it to work. It still runs as before. But I would like to say thank you for the coding tips on the last 2 lines of your code!! Much appreciated!!

Terry
What computer operating system are you using? I didn't know this could happen on newer systems, at least not anymore.
well ... look at this ... i had a problem like this at the beginning
i think your program prints the numbers but there are too many numbers so the screen will go lower and you will see in the console only from 59 and down ...
try not to print so many lines at each loop and print X only once at a loop ...
BHX - I'm using Windows 7 Home Premium 64 Bit

T.O.D. - Being still a complete 'newbie' at C++, I'm not totally familiar with all of the lingo. I'm assuming that the console you refer to is what in the old days we would have called the C:\ prompt (DOS) window? I have scrolled through the entire window after the program runs, and still can't find the 'missing' parts of the program. Is there a way to get the console to show more lines than what it currently does?

Thanks....

Terry
@Signalhill

No, that is what I was referring to in my initial post. Think of it this way

You exceeded the maximum allotted number of lines to display in the console (that dos like window), if I recall correctly for each number you display 4 lines (possibly 5?) - that's quite a bit of space - at least 396 lines.

Hypothetical : if max # of console lines displayable is 300 and you are outputting 396, the first 96 will be truncated and the last 300 will be displayed, hence why I had you change the while loop condition so you could see that your program is functioning properly, it is not an error on your part. It is a limitation of the console's ability to display.
Last edited on
George...

Thanks for explaining that to me!!! I appreciate all of the help that you and the other responders have given me. It's great to know that there are folks out there who are willing to give of their time and knowledge to help out old guys like myself who are trying to learn something new!!

Thanks again all...

Terry
Did it worked after all ?:)) i would be extremely happy to find out that you solved your problem ^_^
Topic archived. No new replies allowed.