Question

[Solved] Meaning of cin.fail() in C++?

while (!correct)
    {   

        cout << "Please enter an angle value => ";
        cin >> value; //request user to input a value

        if(cin.fail()) // LINE 7
        {
            cin.clear(); // LINE 9
            while(cin.get() != 'n'); // LINE 10
            textcolor(WHITE);
            cout << "Please enter a valid value. "<< endl;
            correct = false;

        }
        else
        {
            cin.ignore(); // LINE 18
            correct =true;
        }
    }

Hi, this is part of the code that I have written.
The purpose of this code is to restrict users to input numbers like 10,10.00 etc,
if they input values like (abc,[email protected],etc…) the code will request users to reenter the values.

In order to perform this function( restrict user to input valid valus), I get some tips and guides through forums.

I think is my responsibility to learn and understand what these codes do… since this is the first time I use this code.
can someone briefly explain to me what does the codes in
line 7,9,10, and 18 do? Especially line 10. I got a brief idea on others line just line 10 I don’t know what it did.

Thanks for your guides, I appreciate that!

Solution #1:

cin.fail() tells you if “something failed” in a previous input operation. I beleive there are four recognised states of an input stream: bad, good, eof and fail (but fail and bad can be set at the same time, for example).

cin.clear() resets the state to good.

while(cin.get() != 'n') ; will read until the end of the current input line.

cin.ignore(); will skip until the next newline, so is very similar to while(cin.get() != 'n');.

The whole code should check for end of file too, or it will hang (loop forever with failure) if no correct input is given and the input is “ended” (e.g. CTRL-Z or CTRL-D depending on platform).

Respondent: Mats Petersson

Solution #2:

// LINE 7: cin.fail() detects whether the value entered fits the value defined in the variable.

// LINE 18: cin leaves the newline character in the stream. Adding cin.ignore() to the next line clears/ignores the newline from the stream.

Respondent: fatihk

Solution #3:

For line 7 and line 9, read the document.

while(cin.get() != 'n'); // LINE 10

in the while, it tests whether the line cin.get() is an empty line, i.e, containing just the new line.

Respondent: Yu Hao

Solution #4:

Line 7: test if entered data is correct (can be read as decltype(value)). cin.fail() is always true if some error with the stream happened. Later, in

line 9: you clear cin state from bad to previous, normal state. (recover after the error). You cannot read data anymore until recovering from the bad state.

Line 10: you read until the end of line. Basically you skip one line from the input

Line 18: this line executes only if entered data is corrected. It reads and discards one line from stdin.

Respondent: sasha.sochka

Solution #5:

while(cin.get() != ‘n’): All string in c are null terminated. This means that n is the end of all the string objects. Lets say you have string “this” for c it is thisn, each alphabet being stored in a char type. Please read along
http://www.functionx.com/cpp/Lesson16.htm

cin.fail(): cin.fail() detects whether the value entered fits the value defined in the variable.

read:http://www.cplusplus.com/forum/beginner/2957/

cin.ignore(): Extracts characters from the input sequence and discards them
http://www.cplusplus.com/reference/istream/istream/ignore/

Respondent: virusrocks

Solution #6:

I know it’s not usual in Stack Overflow to just list links, so I’ll give a bit more detail, but this answer really boils down to a bunch of links.

For line 7, just google cin.fail. Here’s a good reference, and what it says:

Returns true if either (or both) the failbit or the badbit error state flags is set for the stream.

At least one of these flags is set when some error other than reaching the End-Of-File occurs during an input operation.

failbit is generally set by an operation when the error is related to the internal logic of the operation itself; further operations on the stream may be possible. While badbit is generally set when the error involves the loss of integrity of the stream, which is likely to persist even if a different operation is attempted on the stream. badbit can be checked independently by calling member function bad:
One line translation: it tells you if there was an unexpected error while trying to read the input stream.

You can find similar references for cin.ignore, cin.clear and cin.get. The quick summary:

cin.ignore – ignore one single character present in the stream.
cin.clear – clear any error flags in the stream
cin.get – get one character at a time, until you hit the newline 'n' character.

Respondent: Scott Mermelstein

Solution #7:

The standard input stream (cin) can fail due to a number of reasons.

For example, if value is an int, and the user enters a large number like
124812471571258125, cin >> value will fail because the number is too big to fit inside an int.

But:

There is a much simpler way to do what you want. You want the user to enter only valid floating-point values, e.g. 10 or 10.00, but no characters, right? So you can just do this:

double value;
cout << "Please enter an angle value: " << endl;
while (!(cin >> value)) { //Since value is a double, (cin >> value) will be true only if the user enters a valid value that can be put inside a double
  cout << "Please enter a valid value:" << endl;
}

This does the same thing that your code does, but much more simply.

If you’re interested in what other things can cause cin to fail, look here:
http://www.cplusplus.com/forum/beginner/2957/

Respondent: maditya

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Most Popular

To Top
India and Pakistan’s steroid-soaked rhetoric over Kashmir will come back to haunt them both clenbuterol australia bossier man pleads guilty for leadership role in anabolic steriod distribution conspiracy