user3449209
user3449209

Reputation:

struggling with cin function c++

i am trying to get a grip on built-in cin functions like cin.good() cin.fail() cin.ignore() cin.clear() etc. but there seems to be a problem with the following code, it works just fine but the line i have commented doesn't show on the runtime prompt. could someone point me to the right direction? :/

using namespace std;

void main()
{
    int x = 0;
    char y = 0;
    int ch = 0;

 do 
    {

    cout << "Press 1 for int" << endl;
    cout << "Press 2 for char" << endl;
    cout << "Press 3 for exit" << endl;

    cout << "enter choice = ";
    cin >> ch;

    if (ch == 1)
    {
        do
        {
            cout << "Enter an integer only = ";
            cin >> x;

            if (!cin.good())
            {
                cout << "Please enter integer only" << endl;
                cin.clear();
                cin.ignore(INT_MAX, '\n');

            }
        } while (cin.good() && x != 1);
            }
    else

        if (ch == 2)
        {
            do
            {
                cout << "enter char only = ";
                cin >> y;

                if (cin.fail())
                {     //the line below doesn't show up! 
                    cout << "Please enter char only" << endl; 
                    cin.clear();
                    cin.ignore(INT_MAX, '\n');
                }


            } while (!cin.fail() && y != 'e');
            }




        } while (ch != 3);
                cout << "End of Program" << endl;
                system("PAUSE");                     // exit(1);


        _getch();

}

Upvotes: 0

Views: 4115

Answers (1)

Sahil Sareen
Sahil Sareen

Reputation: 1834

cin.good()
Returns 0 if the stream has encountered problems such as reading the end of file, non-existent file. and 1 otherwise.
cin.bad()
Returns non-zero value if the stream is totally unusable, e.g. the file cannot be opened (but not if the stream has merely hit the end of the file).
cin.fail()
returns true if the last cin command failed, and false otherwise.
An example

int main() {

int i, j = 0;

while (1) {
  i++;
  cin >> j;
  if (cin.fail()) return 0;
  cout << "Integer " << i << ": " << j << endl;  
 }

} Now suppose you have a text file - input.txt and it's contents are:

  1 2 3 4 5 sahil 6 7

When you will run above short program on that, it will result like:

Integer 1: 1 Integer 2: 2 Integer 3: 3 Integer 4: 4 Integer 5: 5

it will not continue after 5th value as it quits after reading the sixth word, because that is not an integer: cin.fail() holds true.
cin.clear() and cin.ignore()

int age;

for (;;) {
    cout << "Please enter your age: ";
    if (cin >> age) {
        break;
    } else {
        cout << "Please enter a valid integer age" << endl;
        cin.clear();
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
    }
}

Refer this for use in file handling:
bad()

Returns true if a reading or writing operation fails. For example, in the case that we try to write to a file that is not open for writing or if the device where we try to write has no space left.

fail()

Returns true in the same cases as bad(), but also in the case that a format error happens, like when an alphabetical character is extracted when we are trying to read an integer number.

eof()

Returns true if a file open for reading has reached the end.

good()

It is the most generic state flag: it returns false in the same cases in which calling any of the previous functions would return true. Note that good and bad are not exact opposites (good checks more state flags at once).

The member function clear() can be used to reset the state flags.

Upvotes: 2

Related Questions