(The code that has the problem:)
#include <iostream>
usingnamespace std;
int main()
{
int x=99;
while (x>1)
{
cout<< x <<" bottles of beer on the wall. "<<x<<" bottles of beer.\nTake one down, pass it around. "<<(--x)<<" bottles of beer on the wall.\n";
}
cout<< "If that one bottle should happen to fall, what a waste of alcohol!";
}
This code produces the following result:
98 bottles of beer on the wall. 98 bottles of beer.
Take one down, pass it around. 98 bottles of beer on the wall.
In the first line, instead of '99' there is '98'(the first 98). And in second line,instead of '97', there is '98'.
by the way I got the correct answer by the following code. so I need to know what is wrong in the above code(not the suggestions for amendments to make as in the following code):
MY right code:
#include <iostream>
usingnamespace std;
int main()
{
int x=99;
while (x>0)
{
cout<< x <<" bottles of beer on the wall. "<<x<<" bottles of beer.\nTake one down, pass it around. "<<(x-1)<<" bottles of beer on the wall.\n";
x--;
}
cout<< "No more bottles of beer on the ball. What a shame!.";
}
this produces the expected result.
What I did to get the correct answer was to add a If and Else Statement in the while loop.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#include <iostream>
usingnamespace std;
int main()
{
int x=99;
while (x>0)
{
if (x > 1){
cout<< x <<" bottles of beer on the wall. "<<x<<" bottles of beer.\nTake one down, pass it around. "<<(x-1)<<" bottles of beer on the wall.\n" << endl;
x--;
}
else {
cout<< "If that one bottle should happen to fall, what a waste of alcohol!";
x--;
}
}
}
Here the program checks if the value of x is greater than one, if true it prints the chorus.
1 2 3 4
if (x > 1){
cout<< x <<" bottles of beer on the wall. "<<x<<" bottles of beer.\nTake one down, pass it around. "<<(x-1)<<" bottles of beer on the wall.\n" << endl;
x--;
}
Here is the else statement if x is equal to one or below. The "x--" is there to prevent the while loop from continuing after it has been printed, if it wasnt there it would spam the console, the loop ends as the value of x needs to be above zero to function.
1 2 3 4
else {
cout<< "If that one bottle should happen to fall, what a waste of alcohol!";
x--;
}
Thanks for the reply. It is yet another approach for the problem. but like I said, I already got the expected result(the correct code has also been posted above). I want to know what is wrong with the faulty code because it also seems very correct and frankly, more obvious( to make it concise).
could you plz tell me what is wrong with the faulty code?!
cout<< x <<" bottles of beer on the wall. "<<x<<" bottles of beer.\nTake one down, pass it around. "<<(x-1)<<" bottles of beer on the wall.\n" << endl;
The behaviour of this line is undefined.
"--x" may be evaluated before any of the output statements are executed, execution order is not guaranteed.
You should add sequence points to guarantee order of execution, like AustinB did, with the "--x" as a separate statement.
You can explore this further by changing the "x" expressions in the quoted line above and seeing what the output is, the different possibilities in "undefined" may surprise you.
e.g. cout<< x-- <<" bottles of beer on the wall. "<<x--<<" bottles of beer.\nTake one down, pass it around. "<<x--<<" bottles of beer on the wall.\n";
may get you:
97 bottles of beer on the wall. 98 bottles of beer.
Take one down, pass it around. 99 bottles of beer on the wall.
So, I suppose you mean that we cannot include statements like x++, x+=5, etc. (those that overwrites the current value of the variable) in cout statements.
for those (variable overriding statements), we have to write them in a different single statement.
Please correct me if I got it wrong! Any way, thanks for the responses!