Thread: input validation using isalpha and isdigit?

  1. #1
    Registered User
    Join Date
    Jul 2015
    Posts
    1

    input validation using isalpha and isdigit?

    Hello everyone, I am trying to validate the user input by using isalpha and isdigit. The input is supposed to consist of the following format: AB123456. I was going to check for AB using isalpha and 123456 using isdigit. The input needs to consist of two letters and six numbers. I was also going to assign the input to an array so I could check it.

    This is my first time using these, so I'm not too sure how to implement it into my code. I know how to validate an input that is all characters or all digits, but not if it's a combination of the two.

    Here is what I got. It doesn't compile. Can someone point me in the right direction? Thank you:
    Code:
    void searchID ()
    {
     //local variable
     string userInput;
     int count = 0;
     
     do
     {
      cout << endl << "Enter ID number of student to find (or X to exit): ";
      cin >> userInput;
         while((isalpha(userInput[0])) || (isalpha(userInput[1])) ||  (isdigit(userInput[2])) || (isdigit(userInput[3])) || (isdigit(userInput[4])) || (isdigit(userInput[5])) || (isdigit(userInput[6])) || (isdigit(userInput[7])))                      
             {
                cout << "Invalid entry. Try again." << endl;
                cin >> userInput; 
             }
            if (userInput == 'X')
      {
       cout << "Goodbye!" << endl << endl;
       system ("PAUSE");
      }
     }while (userInput != 'X')
     
     return;
    }

  2. #2
    Guest
    Guest
    Using a while loop for the verification looks like a mistake to me. You enter the ID and then you want to verify its format, so an if would be a better choice.

    It would suggest you create a validation function separate from the input handling. Something like:
    Code:
    bool isValid(const string& id)
    {
        return (isalpha(id[0]) && isalpha(id[1]) && isdigit(id[2]) ...etc);
    }

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Have you considered using one or two of the string.find() series of functions instead of the isalpha() and isdigit() (ie: std::string.find_first_not_of())?

    Jim

  4. #4
    Registered User cstryx's Avatar
    Join Date
    Jan 2013
    Location
    Canada
    Posts
    123
    Since this is not an array of a predefined length as far as the std::string typedef is concerned, before just indexing a bunch of random char's, you may consider checking the length() for safety too.
    Code:
    if (str.length() != 8) return false;
    This could be part of the check as well as for safety with the indexing operator, being that if the string length is not 8, there can't be 2 letters and 6 digits in the string.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Preventing wrong input with do-while and isdigit
    By shaddock in forum C Programming
    Replies: 37
    Last Post: 04-30-2014, 04:07 AM
  2. input validation
    By Alexius Lim in forum C++ Programming
    Replies: 5
    Last Post: 12-01-2013, 04:42 AM
  3. how to use isalpha and isdigit?
    By Wei Yoong in forum C Programming
    Replies: 3
    Last Post: 10-27-2012, 12:41 PM
  4. isdigit() to validate if input is number..
    By narendrav in forum C Programming
    Replies: 11
    Last Post: 06-16-2012, 12:16 PM
  5. isdigit help with validation function
    By Zeeth in forum C++ Programming
    Replies: 3
    Last Post: 10-12-2011, 05:25 PM