c++ - CIN validation using cin.fail -
i developing small program asks 4 integers 1 after other using std::cin
. using function request integers, , passing in maximum value allowed argument. check if the value integer use std::cin.fail
. code calls functions shown below.
cout << "specify source number (1 - 1024)\n"; source = validate(1024); cout << "specify destination number (1 - 1024)\n"; // except data, value equal value returned validate function destination = validate(1024); // maximum possible value passed in argument in each of cases. cout << "specify type number (1 - 10)\n"; // user prompted enter desired values. type = validate(10); cout << "source port number (1 - 1024)\n"; port = validate(1024);
and validate function code shown below.
int validate(int max) { int value; // initialise variable hold value. (;;) { // loop forever until correct input entered. cin >> value; if (cin.fail()) { // if cin fails receive value matching declared data type. cout << "characters not permitted.\n"; // notify user of error. cin.clear(); // clear error flag within cin. cin.ignore(10000,'\n'); // ignore newline character in buffer prevent infinite loop. } else if (value > max || value == 0) { cout << "the value have entered not valid, please enter number between 1 , " << max << "\n"; cin.clear(); // clear error flag within cin. cin.ignore(10000, '\n'); // ignore newline character in buffer prevent infinite loop. } else break; } return value; // return validated value. }
this works fine if user enters integer, or letter, validates should, problem having if user enters instance 34d
or 22p
. error saying characters not permitted
exits function , moves onto next request.
tried rearranging , sorts cant seem solve it. appreciated
what want ignore
rest of line if successful:
int validate(int max) { int value; // initialise variable hold value. (;;) { // loop forever until correct input entered. cin >> value; if (cin.fail()) { // if cin fails receive value matching declared data type. cout << "characters not permitted.\n"; // notify user of error. cin.clear(); // clear error flag within cin. cin.ignore(10000,'\n'); // ignore newline character in buffer prevent infinite loop. } else if (value > max || value == 0) { cout << "the value have entered not valid, please enter number between 1 , " << max << "\n"; cin.clear(); // clear error flag within cin. cin.ignore(10000, '\n'); // ignore newline character in buffer prevent infinite loop. } else { cin.ignore(10000, '\n'); // ignore newline character in break; } } return value; // return validated value. }
if think repetitive, can like:
struct ignore_till_eol { ignore_till_eol(std::istream& cin): input(cin) {} ~ignore_till_eol() { input.clear(); input.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); } std::istream& input; }; int validate(int max_value) { for(;;) { ignore_till_eol ignore(cin); int v; if( cin >> v && 0 < v && v <= max_value ) return v; if( cin ) cout << "value invalid, must between 1 , " << max_value << "\n"; else cout << "invalid characters in input\n"; } }
or
int validate(int max_value) { for(;;) { ignore_till_eol ignore(cin); int v; if( !(cin >> v) ) cout << "invalid characters in input\n"; else if( 0 < v && v <= max_value ) return v; else cout << "value invalid, must between 1 , " << max_value << "\n"; } }
Comments
Post a Comment