This does repeat the problem:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
#include <iostream>
int main()
{
using std::cout;
int choice;
choice = 'x';
cout << choice << "\nYour turn\n";
choice = 0;
std::cin >> choice;
if ( 'x' == choice ) cout << "Ho ho ho\n";
cout << "again\n";
choice = 0;
std::cin >> choice;
if ( 'x' == choice ) cout << "Ho hum\n";
cout << "\nToo late\n";
return 0;
}
|
120
Your turn
x
again
Too late |
I did type
x
and it was like it didn't ask the second time.
120
Your turn
120
Ho ho ho
again
x
Too late |
I did type first
120
and then
x
and the second time x!=x, but curiously 120==x.
The problem is:
int choice;
You do read into an integer variable.
x is not an integer and the formatted input sets the stream into state
fail. You have to
clear the state before
any input operation will succeed.
On the system, where I tested my program, the character 'x' has numeric value 120. That is why typing 120 did seem to enter 'x'. The 'x' may have different value in different systems.
Besides, who wants to type 1 or 2 or 120?
You can:
* Use a number (like 0) instead of x
* Read a character '1' or '2' or 'x'
* Do all the necessary magic to successfully mix numeric and character input.
(The last is probably the most educative choice, but are you ready for it?)