Friday 15 August 2014

c++ - Validating input -



c++ - Validating input -

cin >> menuchoice; while (!(cin >> menuchoice)) { cout << "invalid selection, please come in number menu 1" << endl; cin.clear(); cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); }

i want 'catch' string programme doesn't crash or go continuous loop entered above while loop. looks good. until entered 0 menu selection - !(cin >> menuchoice) gets called though valid input (i'll utilize loop ensure within range long it's int should fine, that's thought.

i tried

if (menuchoice == 0) menuchoice = 6;

just before while loop no avail.

any assistance?

i'm looking maintain menu , validation part separate (at moment functions i'll changing them in future classes).

the validation part goes:

while (!(cin >> menuchoice)) { cout << "invalid selection, please come in number menu 1" << endl; cin.clear(); cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); } while (menuchoice < 1 || menuchoice > 4) { cout << "invalid selection, please come in number menu 2" << endl; cin >> menuchoice; //here there problem while (!(cin >> menuchoice)) { cout << "invalid selection, please come in number menu 3" << endl; cin.clear(); cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); } }

while loop 1 - catches initial string input while loop 2 - ensures right range while loop 3 - catches string input user may come in sec time

c++ validation input

No comments:

Post a Comment