c++ - How while(!(cin >> x)) works to re-prompt for input -


while(!(cin >> ar[i]))     {         cin.clear(); // clears bad input         while(cin.get() != '\n')             continue;         cout << "invalid input, please enter valid scores";     } 

the above code larger file. copied bit of code 1 of textbooks , don't feel comfortable using not understand how works.

i using measure handle input errors.

so ar empty array of integers, if decide enter 'k', then

!(cin >> ar[i])  

is true.

from here clear input buffer (i think correct, i'd confirm or dispute please). terminal prints "invalid input..." if press enter nothing happens, isn't enter newline char? shouldn't code read

while(cin.get() == '\n' 

?

while(!(cin >> ar[i])) 

this tries parse value cin , store in ar[i]. default, >> skips whitespace first, sees if characters in cin describe legal value whatever type of ar[i] is. if legal value found, cin stream state remains good, , operator bool() const kick in given boolean not/! operation, such while loop break.

if parsing fails though, stream state set 1 or more of:

  • bad (if there's unrecoverable stream error, stdin supplied on network connection gets disconnected),

  • fail (if characters didn't form legal value type), or

  • eof (end of file, "proper" shutdown/close of input, supplied ^d in unix/linux, ^z in windows, , end of input when program's invoked in echo input | program).

all above modalities described under "state functions" here.

if loop entered due of error conditions above...

{     cin.clear(); // clears bad input 

...this not clear input data stream, clear bad, eof , fail state flags, after further input attempts can made, though stream in bad or eof state reenter state when further input attempted (but not - os may allow successful input after eof conditions std::cin if user types/generates eof code types actual text again...

    while(cin.get() != '\n')         continue; 

this tries read characters terminal until newline \n encountered. idea's clear out rest of presumed unparse-able input might have led fail condition earlier. sadly, if problem was, or becomes, bad or eof condition loop hang program, spinning burning cpu no avail.

    cout << "invalid input, please enter valid scores"; } 

if problem mistyped value , no bad or eof condition, cout prompt further input.


now if press enter nothing happens, isnt enter newline char?

whenever outer loop executing cin >> ar[i] skip whitespace, including newlines type, until sees input (which may need full newline-terminated line flushed terminal or program feeding program), or bad or eof condition. inner while-loop not there rid of empty lines - it's trying discard line presumed non-numeric text in it.


corrected code:

while (!(std::cin >> ar[i])) {     if (std::cin.bad() || std::cin.eof())     {         std::cerr << "fatal error on cin while reading numbers\n";         exit(exit_failure);     }     std::cin.clear();     std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');     std::cout << "invalid input, please enter valid scores\n: "; } 

Comments

Popular posts from this blog

OpenCV OpenCL: Convert Mat to Bitmap in JNI Layer for Android -

android - org.xmlpull.v1.XmlPullParserException: expected: START_TAG {http://schemas.xmlsoap.org/soap/envelope/}Envelope -

python - How to remove the Xframe Options header in django? -