Im kinda newbish right now, but is there a way to stop people from entering the wrong type of input?
If they enter a char for the input the program locks-up. Is there a way to catch those?Code:main() { int a; cout << "Enter a number"; cin >> a; }
This is a discussion on Trapping input within the C++ Programming forums, part of the General Programming Boards category; Im kinda newbish right now, but is there a way to stop people from entering the wrong type of input? ...
Im kinda newbish right now, but is there a way to stop people from entering the wrong type of input?
If they enter a char for the input the program locks-up. Is there a way to catch those?Code:main() { int a; cout << "Enter a number"; cin >> a; }
Last edited by dragon_man; 05-13-2003 at 08:52 PM.
Code:#include <iostream> using namespace std; int main() { int a; bool flag = false; for (;;) { cout << "Enter a number: "; cin >> a; if (cin.fail()) { cin.clear(); cin.ignore(100, '\n'); cout << "Invalid Input!\n"; } else break; } cout << "Your number is: " << a << endl; return 0; }