H
hide2may
Hi There, I'm trying to give some simple math exercises for my kid to
play with but come across the following problem. When the program
comes to "cin >> ans;" it loops forever if the user do not input an
integer (but a char, float, string...whatever). How to solve it,
please?
Thanks in advance, -hide2may
#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
char str[80];
int ans=0, count=1;
cout << "What is your name: ";
gets(str);
cout << "Okay " << str << ", let's do some tests.\n";
while( ans != 31*4 ) {
cout << "(" << count << ") 31 x 4 = ";
cin >> ans; // <-------- Here is the question!
cout << "Wrong! Try again /.\\\n";
}
return 0;
}
play with but come across the following problem. When the program
comes to "cin >> ans;" it loops forever if the user do not input an
integer (but a char, float, string...whatever). How to solve it,
please?
Thanks in advance, -hide2may
#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
char str[80];
int ans=0, count=1;
cout << "What is your name: ";
gets(str);
cout << "Okay " << str << ", let's do some tests.\n";
while( ans != 31*4 ) {
cout << "(" << count << ") 31 x 4 = ";
cin >> ans; // <-------- Here is the question!
cout << "Wrong! Try again /.\\\n";
}
return 0;
}