c++ - trivial regarding isalpha() in while() -


Ignore the unclear number of headers I am using. I'm just copying the code snippet in which I have a problem, and have included every headers because it is hurt.

I'm doing a simple check whether the user input for a desired integer is not character. If the user inputs a character, they will be brought to the beginning of the loop until an integer is entered.

  while (edge ​​langs & lt; 4 || age gender> 12) {// ..}   

ensures that 4 And no number between 12 is recorded, and it works.

My question:

Why is it that when I enter any character, then say 'x', while the content of the loop loop is infinite? I specify the isalpha (edgeLength) is a condition to look for, but there is no luck

  #include & lt; Math.h> // #include for exponents & lt; Iostream & gt; // for cout & lt; & Lt; And cin & gt; & Gt; # Include & lt; Stdlib.h & gt; // for exit () and rand () #include & lt; Ctype.h & gt; // For upper case conversion and include the ILP # & lt; Time.h> // to include random generator # Iomanip & gt; // Formats the board using the namespace STD; Int main () {int edgeLength; Cout & lt; & Lt; "Enter the size board you want between 4 and 12:"; CIN & gt; & Gt; EdgeLength; While (Edge length & lieutenant; 4 || Edge llanation & gt; 12 || Islifa (Edge Lang)) {COAT & LT; & Lt; "Invalid size. Choose a value between 4 and 12:"; CIN & gt; & Gt; EdgeLength; } Return 0; }   

Current output:

  Enter the size board between 4 and 12: E Invalid size Choose a value between 4 and 12: Invalid size Choose a value between 4 and 12: Select a value between an invalid size between 4 and 12: Select a value between an invalid size between 4 and 12: Select an value between an invalid size between 4 and 12: an invalid size between 4 and 12 Choose value: Choose a value between an invalid size of 4 and 12: Choose between a valid size between 4 and 12: Invalid size between 4 and 12 Choose a value: ... ...   

desired output:

  Enter the size board you want, between 4 and 12: E invalid size Choose the value between 4 and 12: 5 [Process Returns 0]    

when Hopefully an int, but can not be converted into input int, it fails and leaves the input data in the stream. Therefore, if you keep reading the same data repeatedly, if you loop, then every time it failed to convert it to an int note that cin & gt; & Gt; If the artist fails, the edge gives a pending glimpse, so you can test it.

In order to handle the situation correctly, with the code snippet, it says:

  # include & lt; Iostream & gt; ; # Include & lt; Border & gt; using namespace std; Int main () {cout & lt; & Lt; "Enter an int:"; Int x = 0; While (! (Cin> & gt; x)) {cin.clear (); Cin.ignore (numeric_limits & lt; streamsys gt; max), '\ n'); Cout & lt; & Lt; "Invalid input. Retry:"; } Cout & lt; & Lt; "You signed in": & lt; & Lt; X & LT; & Lt; Endl; }    

Comments

Popular posts from this blog

c - Mpirun hangs when mpi send and recieve is put in a loop -

python - Apply coupon to a customer's subscription based on non-stripe related actions on the site -

java - Unable to get JDBC connection in Spring application to MySQL -