Thread: Determining whether input is an integer.

  1. #16
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    you are passing a string in the form of text.c_str() to isdigit() when isdigit expects to be passed just a single char.

    You can use any reasonably large value for ignore() and probably get away with it. say cin.ignore(32000). The biggest number you can use, which means the safest value you can use, I believe is call INT_MAX and it is declared in limits.h if your compiler uses the old style header files and climits if it uses the new style header files.

    You can try these miniprograms. I'm sure you can break either one. But you'll have to work at it a bit first, assuming I didn't leave any bugs in them.
    Code:
    #include <iostream>
    #include <climits>
    using namespace std;
    int main()
    {
      int input;
      bool valid = false;
      while(!valid)
      {
        cou << "enter an int" << endl;
        cin >> input;
        if(cin.fail())
        {
           cin.clear();
           cin.ignore(INT_MAX, '\n');
           cout << "invalid input" << endl;
        }
        else
        {
           cout << input << " is valid" << endl;
           valid = true;
        }
      }
       return 0;
    }
    Code:
    #include <iostream>
    #include <cstdlib>
    #include <cstring>
    using namespace std;
    int main()
    {
       char stringInput[256];
       int input;
       bool valid = false;
       int length;
       int i;
       while(!valid)
       {
          cout << "enter an positive integer < 1000" << endl;
          cin >> stringInput;
          length = strlen(stringInput);
          for(i = 0; i < length; ++i)
          {
              if(!isdigit(stringInput[i]))
                  i = 403;
          }
           if(i == length && length < 4)
           {
             valid = true;
             input = atoi(stringInput);
           }
           else 
              cout << "invalid input." << endl;
         }
         cout << input << " is valid" << endl;
         return 0;
    }

  2. #17
    Registered User slaveofthenet's Avatar
    Join Date
    Apr 2003
    Posts
    80
    I'm pretty sure the code I posted will fix your problem. It will get user input untill a valid double value larger than 0 is received. 0.0 will not be accepted, but your code shows you want a value larger than 0. It will however accept values such as 0.01.

  3. #18
    Registered User
    Join Date
    Feb 2003
    Posts
    60
    thanks for all the help you guys. The program now works flawlessly. I used mainly stringstreams to fix my problem but anyways all the loose ends are fixed and it's extremely user firendly.

    Thanks to all again
    C++ can hurt.

  4. #19
    Registered User
    Join Date
    Feb 2004
    Posts
    1

    Talking data type validation

    elad,

    I just want to say thanks for this post, it solved my problem as well. I learned something new as well. I am new to programming and C++. So, thanks for posting a bad a$$ example of data validation!!!!

    rogerg0834

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 03:25 PM
  2. Input class project (again)
    By Elysia in forum C++ Programming
    Replies: 41
    Last Post: 02-13-2009, 10:52 AM
  3. Reading Input from file (Integer array)
    By Govalant in forum C Programming
    Replies: 9
    Last Post: 07-23-2007, 06:13 PM
  4. Reading the input file into integer arrays
    By supaben34 in forum C++ Programming
    Replies: 3
    Last Post: 12-06-2002, 07:04 PM
  5. Reading input as an integer
    By n0de in forum C++ Programming
    Replies: 4
    Last Post: 04-11-2002, 06:52 PM