validation in c++

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • piyush jain
    New Member
    • May 2007
    • 3

    validation in c++

    i ma trying to check that my input is always an int.
    if it w'll be a char it will genertae an error
  • scruggsy
    New Member
    • Mar 2007
    • 147

    #2
    Originally posted by piyush jain
    i ma trying to check that my input is always an int.
    if it w'll be a char it will genertae an error
    Using standard console I/O?
    One way would be to use cin to read an integer and check the return value of cin for error, like:
    Code:
    int num;
    if (!(cin >> num))
      cout << "ERROR";

    Comment

    • RedSon
      Recognized Expert Expert
      • Jan 2007
      • 4980

      #3
      Originally posted by scruggsy
      Using standard console I/O?
      One way would be to use cin to read an integer and check the return value of cin for error, like:
      Code:
      int num;
      if (!(cin >> num))
        cout << "ERROR";
      Thats not going to validate that your input is an int.

      Comment

      • RedSon
        Recognized Expert Expert
        • Jan 2007
        • 4980

        #4
        Originally posted by piyush jain
        i ma trying to check that my input is always an int.
        if it w'll be a char it will genertae an error
        The problem is chars are stored as ints. A char 'Z' is the same as int 90 if you are using ASCII. If you want to see if your input is not a char you would need to see if there is a standard i/o that will allow you to check or you can do something like

        if (input > 'A' && input <= 'z')
        then you know you have a char

        Comment

        • weaknessforcats
          Recognized Expert Expert
          • Mar 2007
          • 9214

          #5
          Not true:

          Originally posted by RedSon
          if (input > 'A' && input <= 'z')
          then you know you have a char
          You know you have a char when you cin >> to a char variable.

          I realize that an A is 65 but if you cin >> int variable, the >> fails because A is not interger. Integer values for cin must be digits only 0 - 9.

          Here is a better way to get an int:

          [code=cpp]
          bool GetInt(int& data); //function prototype

          int main()
          {
          int test;
          bool rval;
          while (1) //infinite loop. Need CTRL+C to stop program
          {
          rval = GetInt(test);
          if (rval)
          {
          cout << "The integer is: " << test << endl;
          }
          else
          {
          cout << "Error! Eating an input character" << endl;
          //You could use cin.get() here to see what you ate
          cin.ignore();
          }

          }

          return 0;

          }

          bool GetInt(int& data)
          {
          cin >> data;
          if (cin.good())
          {
          return true;
          }
          else
          {
          cin.clear(); //clear the fail bit that got us here
          return false;
          }
          }
          [/code]

          Here the cin >> data (an int) is followed by a test of the goodbit. In the case of the A, the goodbit is false. The istream is now in a fail state and the A is still in the input buffer. In this case you clear() the fail state. The A is still in the buffer.

          So you have to eat the A and try again.

          Comment

          • RedSon
            Recognized Expert Expert
            • Jan 2007
            • 4980

            #6
            Originally posted by weaknessforcats
            Not true:



            You know you have a char when you cin >> to a char variable.

            I realize that an A is 65 but if you cin >> int variable, the >> fails because A is not interger. Integer values for cin must be digits only 0 - 9.

            Here is a better way to get an int:

            [code=cpp]
            bool GetInt(int& data); //function prototype

            int main()
            {
            int test;
            bool rval;
            while (1) //infinite loop. Need CTRL+C to stop program
            {
            rval = GetInt(test);
            if (rval)
            {
            cout << "The integer is: " << test << endl;
            }
            else
            {
            cout << "Error! Eating an input character" << endl;
            //You could use cin.get() here to see what you ate
            cin.ignore();
            }

            }

            return 0;

            }

            bool GetInt(int& data)
            {
            cin >> data;
            if (cin.good())
            {
            return true;
            }
            else
            {
            cin.clear(); //clear the fail bit that got us here
            return false;
            }
            }
            [/code]

            Here the cin >> data (an int) is followed by a test of the goodbit. In the case of the A, the goodbit is false. The istream is now in a fail state and the A is still in the input buffer. In this case you clear() the fail state. The A is still in the buffer.

            So you have to eat the A and try again.
            duh, I forgot cin does more then just read bits from the input stream. Its been so long since I did console apps like that. Guess its time to review the basics.

            Comment

            Working...