SSN program.

Prompt the user for a social security number. Determine if the SSN is in the correct format.
correct input:
Standard Input
123-45-67890
Program Output
Enter your SSN in this format: XXX-XX-XXXX
Problem: You must type exactly 11 characters.

wrong input:
Standard Input
123-bb-6789
Program Output
Enter your SSN in this format: XXX-XX-XXXX
Problem: Only digits are allowed in a SSN

Standard Input
--123456789
Program Output
Enter your SSN in this format: XXX-XX-XXXX
Problem: The dashes are missing or are in the wrong spot

this is what i ahve so far.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

#include <iostream>
using namespace std;

int main()
{
    int ssn;

    cout << "Enter your SSN: XXX-XX-XXXX" << endl;
    cin >> ssn;

    if(ssn <= 000000000 || ssn >= 999999999)
    {
        cout << "valid ssn." << endl;
    }
    else
    {
        cout << "wrong ssn." << endl;
    }

}
1.Put the input into a string.
2.Check the length of the string
3.Check the number of digits in the string.
4. Check the dash locations. Which will be located in element 4 and element 7 given the input is correct.
The type cannot be int. It could be a string. Three digits, dash, two digits, dash, and four digits. Regular expression could be used.
should i use a for loop?
sigh, this is due tomorrow, please may you give me the code, or something to get started.
Cody's approach is nice. If you cannot do steps 1, 2 and 4, then you really should study the basics more.

For the step 3 a loop makes sense, although one could hide it into library:
http://www.cplusplus.com/reference/algorithm/count_if/

http://www.cplusplus.com/reference/cctype/isdigit/
i can do the first three steps.
Topic archived. No new replies allowed.