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;
}
Printable View
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;
}
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;
}
Thanks