77895

Validating input

Question:

cin >> menuChoice; while (!(cin >> menuChoice)) { cout << "invalid selection, please enter a number from the menu 1" << endl; cin.clear(); cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); }

I want to 'catch' a string so that the programme doesn't crash or go into a continuous loop so I entered the above while loop. Looks good. That is until I entered 0 into menu choice - the !(cin >> menuChoice) gets called even though it is valid input (I'll use another loop to ensure it is within range but as long as it's an int it should be fine, that's what I thought.

I even tried a

if (menuChoice == 0) menuchoice = 6;

just before the while loop but to no avail.

Any assistance?

Answer1:

I'm looking to keep the menu and the validation part separate (at the moment its functions but I'll be changing them in the future to classes).

The validation part goes:

while (!(cin >> menuChoice)) { cout << "invalid selection, please enter a number from the menu 1" << endl; cin.clear(); cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); } while (menuChoice < 1 || menuChoice > 4) { cout << "invalid selection, please enter a number from the menu 2" << endl; cin >> menuChoice; //here is where there is a problem while (!(cin >> menuChoice)) { cout << "invalid selection, please enter a number from the menu 3" << endl; cin.clear(); cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); } }

while loop 1 - catches any initial string input while loop 2 - ensures correct range while loop 3 - catches any string input the user may enter a second time

Answer2:

turns out it is just

cin>> menuChoice; while (!cin) { cout << "invalid selection, please enter a number from the menu 1" << endl; cin.clear(); cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); cin>> menuChoice; }

that is needed (as well as the rest of the code etc)

Recommend

  • C++ writing to file producing garbage data [closed]
  • Decompress file from Boost filtering_streambuf to std::vector?
  • How to `pause`, and `resume` download work?
  • Cannot cin.ignore till EOF?
  • Application Menu Items Xcode
  • C++ if(!cin) causes loop
  • Handling exception logging in a Java task scheduler program?
  • Drop variable in panel data in R conditional based on a defined number of consecutive observations
  • How to convert one data type to another
  • Why can shapely/geos parse this 'invalid' Well Known Binary?
  • How to send None with Signals across threads?
  • Get MP3 ID3 meta data and song duration using AudioStreamer
  • Error in GGally: Error in unit(tic_pos.c, “mm”) : 'x' and 'units' must have leng
  • CRASH: *** -[__NSArrayM objectAtIndex:]: index 4294967295 beyond bounds [0 .. 9]
  • Tap to record like in vine using javacv
  • RxJava debounce by arbitrary value
  • D3 get axis values on zoom event
  • Unable to decode certificate at client new X509Certificate2()
  • C: Incompatible pointer type initializing
  • Converting a WriteableBitmap image ToArray in UWP
  • Is there a way to do normal logging with EureakLog?
  • Reading JSON from a file using C++ REST SDK (Casablanca)
  • Why value captured by reference in lambda is broken? [duplicate]
  • Java Scanner input dilemma. Automatically inputs without allowing user to type
  • Apache 2.4 and php-fpm does not trigger apache http basic auth for php pages
  • javascript inside java/jsp code
  • what is the difference between the asp.net mvc application and asp.net web application
  • Release, debug version and Authorization Google?
  • How to get next/previous record number?
  • Why winpcap requires both .lib and .dll to run?
  • AT Commands to Send SMS not working in Windows 8.1
  • Is there a mandatory requirement to switch app.yaml?
  • Matrix multiplication with MKL
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • How to set the response of a form post action to a iframe source?
  • apache spark aggregate function using min value
  • Are Kotlin's Float, Int etc optimised to built-in types in the JVM? [duplicate]
  • unknown Exception android
  • Sorting a 2D array using the second column C++
  • Binding checkboxes to object values in AngularJs