[Subject Prev][Subject Next][Thread Prev][Thread Next][Subject Index][Thread Index]

Problem with cin



Hi ,
      Have a look at the code.
 If  I give some invalid value for one integer (like
characters) , no values are read into both . Ideally , if
reading int1 fails then it should print some message and try
reading int2.Am I missing something?

#include <iostream.h>
main()
{
  int int1=0;
  int int2=0;
  cout << " Enter the int1 val : " << flush;
  cin >> int1;
  if(!cin)
  {
   cout  << endl << "Error reading int1" << endl << flush;
   cin.clear();
  }

  cout << " Enter the int2 val : " << flush;
  cin >> int2;

  if(!cin)
  {
    cout  << endl << "Error reading int2" << endl << flush;
    cin.clear();
  }

  cout << endl << " " << int1 << " " << int2 << flush;
}