Testing error states. : cin « Development « C++ Tutorial






#include <iostream>
using std::cin;
using std::cout;
using std::endl;

int main()
{
   int integerValue;

   // display results of cin functions
   cout << "Before a bad input operation:"
      << "\ncin.rdstate(): " << cin.rdstate()
      << "\n    cin.eof(): " << cin.eof()
      << "\n   cin.fail(): " << cin.fail()
      << "\n    cin.bad(): " << cin.bad()
      << "\n   cin.good(): " << cin.good();

   cin >> integerValue;
   cout << endl;

   cout << "After a bad input operation:"
      << "\ncin.rdstate(): " << cin.rdstate()
      << "\n    cin.eof(): " << cin.eof()
      << "\n   cin.fail(): " << cin.fail()
      << "\n    cin.bad(): " << cin.bad()
      << "\n   cin.good(): " << cin.good() << endl << endl;

   cin.clear();

   cout << "After cin.clear()" << "\ncin.fail(): " << cin.fail()
      << "\ncin.good(): " << cin.good() << endl;
   return 0;
}
Before a bad input operation:
cin.rdstate(): 0
    cin.eof(): 0
   cin.fail(): 0
    cin.bad(): 0
   cin.good(): 12

After a bad input operation:
cin.rdstate(): 0
    cin.eof(): 0
   cin.fail(): 0
    cin.bad(): 0
   cin.good(): 1

After cin.clear()
cin.fail(): 0
cin.good(): 1








5.1.cin
5.1.1.Read int value from keyboard
5.1.2.cin Handles Different Data Types
5.1.3.Use get() to read a string that contains spaces
5.1.4.Use the extraction operator >> with cin.get( ) to process an entire string.
5.1.5.cin with strings (cin.getline)
5.1.6.cin and atoi, atof functions
5.1.7.Using peek() and putback()
5.1.8.Concatenate put()
5.1.9.Testing error states.
5.1.10.Unformatted I/O using cin.read, cin.gcount and cout.write
5.1.11.Contrasting input of a string via cin and cin.get
5.1.12.Demonstrating member function width
5.1.13.Read char array from keyboard, get its length and concatenate two strings
5.1.14.Read a character from keyboard
5.1.15.Copy all standard input to standard output
5.1.16.Manipulator that skips until end-of-line
5.1.17.avoids buffer overflow with cin.width
5.1.18.Extends std::streambuf to create data buffer
5.1.19.Using member functions get, put and eof.
5.1.20.Contrasting input of a string with cin and cin.get.
5.1.21.Unformatted I/O with read, gcount and write.
5.1.22.Character input with member function getline.
5.1.23.Is it a bad input