c++ - cin fails to stop while loop -
I was wondering if you can tell me why this loop can not find if I say 'r' value ' Value of
Double Values std :: cout & lt; & Lt; "Please enter a real number:"; Std :: cin & gt; & Gt; value; While (! Isdigit (value)) {std :: cout & lt; & Lt; "Sorry, but only numbers are valid. \ NPlease enter a real number:"; Std :: cin & gt; & Gt; value; }
Thanks a lot.
You should not be using isdigit ()
because it To test whether a single char
type is an ASCII digit.
If you are expecting a user to enter a real number (and not a string), you can use cin.fail ()
to use your code. should do. If cin
is expecting a double
, then reads a string, then cin.fail ()
return true
.
Another (cleaner) option is to test the status of cin
, as @bangfs reported:
While (
Comments
Post a Comment