Program to display SSN.

Good evening, basically I have a program that requires the following:

- Prompt the user to enter a SSN.
- Verify that it is in the correct format. xxx-xx-xxxx
- Verify that only digits are entered (no letters) for example a20-66-xx22 would give an error.
- Verify that the dashes are in the correct place.

I've been able to get pretty much everything done with the exception of the verification that there are no letters entered, no matter what I do it gives me an error. I have the input and length of the string, checked the number of digits and dash location, no problem, it's only the validation so there's only numbers and letters where I'm breaking my head trying to figure out.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

const int SSN_LENGTH = 11;

int main ()
{
    string Social (string userSocial);
	string userSocial;
	bool check = false;

cout << "Enter your SSN in this format: XXX-XX-XXXX" << endl;
cin >> userSocial;

if (userSocial.length () != SSN_LENGTH)
    {
        cout << "Problem: You must type exactly 11 characters." << endl;
        return 0;
    }
/* my problem is here, tried loops, ifs, string comparison and I still
 can't get it done.  Everything else works the way I want it to work.
    {
        cout << "Problem: Only digits are allowed in a SSN" << endl;
        return 0;
    }
*/
if (userSocial.length() == SSN_LENGTH)
{

    if (userSocial[3] == '-' && userSocial[6] == '-')
        {
        for (int i = 0; i < userSocial.length(); i++)
            {
            if (isdigit (userSocial [i]))
                {
                check = true;
                }
            }
        }
}
if (check == true)
{
cout << "That is valid." << endl;
}
if (check == false)
{
cout << "Problem: The dashes are missing or are in the wrong spot" << endl;
}
return 0;
}


I'm trusting the user on the other thread to not rip off this code.
Last edited on
I would change line 13 from bool check = false; to bool check = true;

Then lines 37-40 to
1
2
3
4
5
if ( !isdigit (userSocial [i]) )
{
    check = false;
    break;
}


By your logic if someone types w34-45-5678 then it will not error out. In first pass the check wont become true, but second pass it will and this SSN will be accepted which is wrong. Above code should fix this. Also, this way the loop breaks the moment it finds a non-digit.

C++11 way of the for loop will be
1
2
3
4
5
6
7
8
for( const auto &chr:userSocial )
{
    if(!isdigit(chr))
    {
        check = false;
	break;
    }
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>
//#include <iomanip>
#include <string>
#include <cctype> // for std::isdigit()

//using namespace std;

const int SSN_LENGTH = 11;

int main ()
{
    std::string ssn;

    std::cout << "Enter your SSN in this format: XXX-XX-XXXX: " ;
    std::cin >> ssn;

    if (ssn.length () != SSN_LENGTH)
    {
        std::cout << "Problem: You must type exactly 11 characters.\n" ;
        return 1; // return 1 on error
    }

    if( ssn[3] != '-' || ssn[6] != '-' )
    {
        std::cout << "Problem: inorrect placement of dash.\n" ;
        return 1 ; // return 1 on error
    }

    for( int i = 0 ; i < SSN_LENGTH ; ++i )
    {
        if( i==3 || i==6 ) continue ; // position for dash, skip this character

        if( !std::isdigit( ssn[i] ) ) // if this is not a decimal digit
        {
           std::cout << "Problem: non-digit character in number.\n" ;
           return 1 ; // return 1 on error
        }
    }

    std::cout << "No problem: " << ssn << " is in the right format\n" ;
    // implicit return 0 on no error
}
closed account (j3Rz8vqX)
Alternative to std::isdigit():
1
2
3
4
5
        if(ssn[i]<'0' || ssn[i]>'9') // if this is not a decimal digit
        {
           std::cout << "Problem: non-digit character in number.\n" ;
           return 1 ; // return 1 on error
        }
Doh, I can see what I did wrong. Thanks guys, it's perfect and it's working.
Topic archived. No new replies allowed.