Can you please tell us how best to check the variable for overflow when entering it from the console
For example for the code
int i; cin >> i; cout << i << endl; Can you please tell us how best to check the variable for overflow when entering it from the console
For example for the code
int i; cin >> i; cout << i << endl; The error can be determined either through the good () function or simply by checking the stream in the conditional statement. But after determining that an error has occurred, it is necessary to remove errors from the stream using the clear () and ignore () functions, otherwise the subsequent input will not work:
int a; cout<<"Input a="; cin>>a; if( cin.good() ) // if ( cin ) { cout<<"Correct input a="<<a<<endl; } else { cout<<"Error input"<<endl; cin.clear(); cin.ignore(); } int b; cout<<"Input b="; cin>>b; cout<<"b="<<b<<endl; An example of the result of the program: 
If you remove the clear, the lines clear () and ignore (), then the subsequent input to the variable b flies without stopping. An example of such work: 
Solved the problem using after checking input
if (cin.good()) Source: https://ru.stackoverflow.com/questions/602374/
All Articles