Hello, I am fairly new to programming and found a set of beginner-level problems in this forum (
http://www.cplusplus.com/forum/articles/12974/ ).
I have a question about this problem:
While( user == gullible )
Requires:
variables, data types, and numerical operators
basic input/output
logic (if statements, switch statements)
loops (for, while, do-while)
Write a program that ccontinues to asks the user to enter any number other than 5 until the user enters the number 5.
Then tell the user "Hey! you weren't supposed to enter 5!" and exit the program.
★ Modify the program so that after 10 iterations if the user still hasn't entered 5 will tell the user "Wow, you're more patient then I am, you win." and exit.
★★ Modify the program so that it asks the user to enter any number other than the number equal to the number of times they've been asked to enter a number. (i.e on the first iteration "Please enter any number other than 0" and on the second iteration "Please enter any number other than 1"m etc. etc. The program must behave accordingly exiting when the user enters the number they were asked not to.)
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 28 29 30 31 32 33 34 35 36 37 38
|
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main()
{
int number;
int j = 0;
string character;
cout << "Please enter any number other than 5: ";
while (true)
{
getline(cin,character);
stringstream temp(character);
if (temp >> number)
{
while (number != 5)
{
while (j < 9)
{
cin >> number;
j++;
}
cout << "I give up.";
return 0;
}
cout << "You win!";
break;
}
else
{
cout << "You did not enter a number. Please try again: ";
}
}
return 0;
}
|
__________________________________________________________
Now, the problem when I run the program is this:
Please enter any number other than 5:
1
2
3
a
I give up.
Process returned 0 (0x0) execution time : 2.969 s
Press any key to continue.
__________________________________________________________
After I input a number, everything works fine until I input a character.
Any help on this one?