I was wondering if anyone could offer some suggestions on how to validate a social security number & their employee # that a user would input separately through prompts?

The SS# would be in the form of xxx-xx-xxxx where the x's are 0-9. I tried using this function that I wrote

bool isInt(string mystring)
{
int i;

for (i=0;i<mystring.length();i++)
{
   if (!isdigit(mystring[i]))
    return false;

    else
    return true;

and this code in my main to do it

cout<<"Please enter your social security number: ";
getline(cin, socialSecurity);
cout<<endl;

//input validation
result = isInt(socialSecurity);

if  (!result)
    {cout<<"Your social security number isn't valid."<<endl; 
    cout<<"Please enter a valid number."<<endl;
    cout<<endl;
    }
    while (!result);

but it still just lets me type in whatever into socialSecurity, including letters.

The employee number is xxx-L, where x is 0-9 and L can be any letter from A-M.

Thanks!

I was wondering if anyone could offer some suggestions on how to validate a social security number & their employee # that a user would input separately through prompts?

The SS# would be in the form of xxx-xx-xxxx where the x's are 0-9. I tried using this function that I wrote

bool isInt(string mystring)
{
int i;

for (i=0;i<mystring.length();i++)
{
   if (!isdigit(mystring[i]))
    return false;

    else
    return true;

and this code in my main to do it

cout<<"Please enter your social security number: ";
getline(cin, socialSecurity);
cout<<endl;

//input validation
result = isInt(socialSecurity);

if  (!result)
    {cout<<"Your social security number isn't valid."<<endl; 
    cout<<"Please enter a valid number."<<endl;
    cout<<endl;
    }
    while (!result);

but it still just lets me type in whatever into socialSecurity, including letters.

The employee number is xxx-L, where x is 0-9 and L can be any letter from A-M.

Thanks!

You are returning true too early here:

for (i=0;i<mystring.length();i++)
{
   if (!isdigit(mystring[i]))
    return false;

    else
    return true;

This only checks the first character of mystring . Return true only after the for-loop has completed. Remove the else part of the if statement and put return true after the for-loop is over without any else statement. That's that part of the problem, I think. Are you checking for hyphens anywhere?

Since there are hyphens in the string that function won't work because it will return false on detecting the first hyphen. You need to check that the string consists of 3 digits, hyphen, 2 digits, hyphen, and 4 digits, and that all the digits are not zero.

Or you could buy for $24.99 this library. :) But I doubt that your instructor wants you to go that far in validating SSN.

Since I can't use isInt because of the two dashes, how about some type of input validation based on substring position?

Since I can't use isInt because of the two dashes, how about some type of input validation based on substring position?

That's the idea. Fix the location of your return true statement, then add to it to check for length, hyphens, and digits, all in the right place. I hadn't known about no zeroes in the SS#, but you can further check for that. Figure out on paper exactly what a good SS# is and what it isn't, figure out what to test for, and test for all of it. If any test flunks, return false. Otherwise return true.

I'm trying this code, but I'm getting a lot of no match for 'operator!=' in 'std::basic_string errors.

void Employee::setSocialSecurity()
{
     do
     {
     cout<<"Please enter your social security number: ";
     getline(cin, socialSecurity);
     cout<<endl;
     //input validation
     if (socialSecurity.substr(0,2) != 0-9 && socialSecurity.substr(3) != "-" &&        socialSecurity.substr(4,5) != 0-9 && socialSecurity.substr(6) != "-" && socialSecurity.substr(7,10) != 0-9)
     cout<<"Please enter a valid social security number."<<endl;
     cout<<endl;
     }
     while (socialSecurity.substr(0,2) != 0-9 && socialSecurity.substr(3) != "-" && socialSecurity.substr(4,5) != 0-9 && socialSecurity.substr(6) != "-" && socialSecurity.substr(7,10) != 0-9);
     
     }

You can use != but you can't use it the way you are trying to use it. There are no quotes in your implementation and there needs to be:

socialSecurity.substr(0,2) != 0-9  // error

Here is a program that works that compares strings using ==, !=, and compare:

#include <iostream>
#include <string>
using namespace std;

int main ()
{
    string animal = "dog";
    string animal2 = "cat";
    
    if (animal == "dog")
        cout << "woof\n";
    else
        cout << "meow\n";
    
    if (animal2 != "dog")
        cout << "meow\n";
    else
        cout << "woof\n";
    
    if (animal.compare (animal2) == 0)
        cout << "They are the same animals.\n";
    else
        cout << "They are different animals.\n";
        
    cin.get ();        
    return 0;
}

It looks like you are trying to use a regular expression or something. I'm not sure. I would isolate the character, then check whether it is a digit using isdigit from cctype:

http://www.cplusplus.com/reference/clibrary/cctype/isdigit.html

I was originally going to use isdigit but it was said that as soon as isdigit finds a -, it will return false because - isn't a digit.

I went back to my notes on subscripts and have since modified my code to properly use subscripts ie if I want a range of four characters starting at position 7 to 10, it would be (7,4) not (7,10) like in python.

I've also made it much more specific to test it with just 0 and 1's but it's still not working when I try 000-11-0101 it just keeps repeating my not valid message. Here is the updated code...

thx

{
     do
     {
     cout<<"Please enter your social security number: ";
     getline(cin, socialSecurity);
     cout<<endl;
     //input validation
     if (socialSecurity.substr(0,3) != "0" || "1" && socialSecurity.substr(3) != "-" && socialSecurity.substr(4,2) != "0" || "1" && socialSecurity.substr(6) != "-" && socialSecurity.substr(7,4) != "0" || "1")
     cout<<"Please enter a valid social security number."<<endl;
     cout<<endl;
     }
     while (socialSecurity.substr(0,3) != "0" || "1" && socialSecurity.substr(3) != "-" && socialSecurity.substr(4,2) != "0" || "1" && socialSecurity.substr(6) != "-" && socialSecurity.substr(7,4) != "0" || "1");
     
     }

I was originally going to use isdigit but it was said that as soon as isdigit finds a -, it will return false because - isn't a digit.

I went back to my notes on subscripts and have since modified my code to properly use subscripts ie if I want a range of four characters starting at position 7 to 10, it would be (7,4) not (7,10) like in python.

I've also made it much more specific to test it with just 0 and 1's but it's still not working when I try 000-11-0101 it just keeps repeating my not valid message. Here is the updated code...

thx

{
     do
     {
     cout<<"Please enter your social security number: ";
     getline(cin, socialSecurity);
     cout<<endl;
     //input validation
     if (socialSecurity.substr(0,3) != "0" || "1" && socialSecurity.substr(3) != "-" && socialSecurity.substr(4,2) != "0" || "1" && socialSecurity.substr(6) != "-" && socialSecurity.substr(7,4) != "0" || "1")
     cout<<"Please enter a valid social security number."<<endl;
     cout<<endl;
     }
     while (socialSecurity.substr(0,3) != "0" || "1" && socialSecurity.substr(3) != "-" && socialSecurity.substr(4,2) != "0" || "1" && socialSecurity.substr(6) != "-" && socialSecurity.substr(7,4) != "0" || "1");
     
     }

You don't test the dashes with isdigit. You don't test strings with isdigit. You test characters with isdigit. You know where the digits are supposed to be and where they are not supposed to be. Test for the appropriate length (11), test for dashes at spots 3 and 7, and test for digits everywhere else using isdigit.

socialSecurity.substr(4,2) != "0"

This will always be true. A string 3 characters long will always be unequal to a string one character long. It looks like you are trying to check for the integer value of the string, which isn't what you are actually doing. You can do that with the atoi function, but I'd stick with comparing it character by character using isdigit if it is supposed to be a digit or comparing it to a hyphen if it is index 3 or 7 in the Social security number. I think that's the easiest way. Using substrings and atoi would probably be the second easiest way.

I'm almost there! I got it down to just one compiler error...expected primary expression before != token on the if statement. I checked all of the ( ) and they match up. Any idea's? Thanks.

do
     {
     cout<<"Please enter your social security number: ";
     getline(cin, socialSecurity);
     cout<<endl;
     //input validation
       if (socialSecurity.length()) != 11 && (isdigit!(socialSecurity.substr(0,2))) && (socialSecurity.substr(3)) != "-" && (isdigit!(socialSecurity.substr(4,5))) && (socialSecurity.substr(6)) != "-" && (isdigit!(socialSecurity.substr(7,10))) 
       cout<<"Please enter a valid social security number."<<endl;
     }
     while (socialSecurity.length()) != 11 && (isdigit!(socialSecurity.substr(0,2))) && (socialSecurity.substr(3)) != "-" && (isdigit!(socialSecurity.substr(4,5))) && (socialSecurity.substr(6)) != "-" && (isdigit!(socialSecurity.substr(7,10)));

I'm almost there! I got it down to just one compiler error...expected primary expression before != token on the if statement. I checked all of the ( ) and they match up. Any idea's? Thanks.

do
     {
     cout<<"Please enter your social security number: ";
     getline(cin, socialSecurity);
     cout<<endl;
     //input validation
       if (socialSecurity.length()) != 11 && (isdigit!(socialSecurity.substr(0,2))) && (socialSecurity.substr(3)) != "-" && (isdigit!(socialSecurity.substr(4,5))) && (socialSecurity.substr(6)) != "-" && (isdigit!(socialSecurity.substr(7,10))) 
       cout<<"Please enter a valid social security number."<<endl;
     }
     while (socialSecurity.length()) != 11 && (isdigit!(socialSecurity.substr(0,2))) && (socialSecurity.substr(3)) != "-" && (isdigit!(socialSecurity.substr(4,5))) && (socialSecurity.substr(6)) != "-" && (isdigit!(socialSecurity.substr(7,10)));
isdigit!(socialSecurity.substr(7,10))

Problem 1 - ! goes before isdigit, not after:

!isdigit (/* character */)  // correct
isdigit! (/* character */) // incorrect

Problem 2 - isdigit takes a character, not a string:

!isdigit (socialSecurity[7])  // correct
!isdigit (/* string */)  // incorrect

Problem 3 - make sure you don't have && and || mixed up. Think about exactly what you want to do.

General approach. Test in order. If any test fails, the SS# fails.

bool goodSSN = true;

if (/* test for length != 11 */)
     goodSSN = false;
else if (socialSecurity[3] != '-' || socialSecurity[6] != '-')
     goodSSN = false;
else
{
     // test the remaining characters character by character
     // using isdigit.  If any fail test, flag goodSSN as false.
     // using a for-loop in here will be helpful, but you can also
     // have an if statement for each of the 9 characters.  A for-loop
     // is preferred though.

     for (int i = 0; i < 11; i++)
     {
          if (i != 3 && i != 6) // already checked dashes
          {
                // isolate ith character, test using isdigit
                // using another if statement
          }
     }
}

// if goodSSN is true at this point, SS# is valid.  Otherwise invalid.

That's one way to do it

Thanks that worked perfectly!

Now I'm going to adapt it for the employee number.

xxx-L

x = 0-9

L = A-M

http://www.asciitable.com/

Consider the fact that A - M is 65 through 77 in the Ascii table, so you can use that to your advantage:

char letter;
cout << "Enter a letter: ";
cin >> letter;
if (letter < 65 || letter > 77)
     cout << "A through M not entered" << endl;
else
     cout << "A through M entered" << endl;

DOH@! Already wrote it and turned it in. Thanks though...maybe for next time!

:)

Here's my code...

bool validateEmployNum(string EmployNum)
{
bool goodEmployNum = true;
int i;


if (EmployNum.length() != 5)
     goodEmployNum = false;
     
else if (EmployNum[3] != '-')
     goodEmployNum = false;
     
else if (EmployNum[4] != 'A' && EmployNum[4] != 'B' && EmployNum[4] != 'C' && EmployNum[4] != 'D' && EmployNum[4] != 'E' && EmployNum[4] != 'F' && EmployNum[4] != 'G' && EmployNum[4] != 'H' && EmployNum[4] != 'I' && EmployNum[4] != 'J' &&  EmployNum[4] != 'K' &&  EmployNum[4] != 'L' &&  EmployNum[4] != 'M')
     goodEmployNum = false;
     
     
else 
{     
     for (int i = 0; i <= 2; i++)
     {
         if (!isdigit(EmployNum[i]))
            goodEmployNum = false;

             }
                          
}

return goodEmployNum;
}

Thanks to everyone and VernonDozier since I based this code off of your example as well.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.