Here is the code running GNU GCC C++ on linux.
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
int magic;
int guess;
magic = rand();
do {
cout << "Enter your guess: ";
cin >> guess;
if (guess == magic) {
cout << "** Right ** ";
cout << magic << " is the number.\n";
}
else {
cout << "...Sorry, you are wrong.";
if (guess > magic)
cout << " Your guess is too high.\n";
else
cout << " Your guess is too low.\n";
}
} while(guess != magic);
return 0;
}
End of program
cout << " Your guess is too low.\n"; is the runaway code and is executing over and over again without doing the while test when guess is < magic.
guess >= magic it works ok.
Is this a compiler bug or did I code something wrong?
Thanks,
Kent
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
int magic;
int guess;
magic = rand();
do {
cout << "Enter your guess: ";
cin >> guess;
if (guess == magic) {
cout << "** Right ** ";
cout << magic << " is the number.\n";
}
else {
cout << "...Sorry, you are wrong.";
if (guess > magic)
cout << " Your guess is too high.\n";
else
cout << " Your guess is too low.\n";
}
} while(guess != magic);
return 0;
}
End of program
cout << " Your guess is too low.\n"; is the runaway code and is executing over and over again without doing the while test when guess is < magic.
guess >= magic it works ok.
Is this a compiler bug or did I code something wrong?
Thanks,
Kent