if i enter no numerical chars a cin error occursCode:...
int choice;
cin >> choice;
...
that i can't restore with cin.clear()
i detect the error with cin.fail()
is it a fatal error?
TIA
Printable View
if i enter no numerical chars a cin error occursCode:...
int choice;
cin >> choice;
...
that i can't restore with cin.clear()
i detect the error with cin.fail()
is it a fatal error?
TIA
Try something like this one:
Code:#include <iostream>
int main()
{
int i;
std::cout <<"Enter a number" <<std::endl;
while (!(std::cin>>i))
{
std::cout <<"Again:" <<std::endl;
std::cin.clear();
while (std::cin.get() != '\n');
}
std::cout <<"You entered " <<i <<std::endl;
}
or take out all the std:: and putat the beginingCode:using namespace std;
.. and for those interested, you can learn a little more about namespaces here