Assignment Assistance

Here's the question my instructor is asking as to code.
"A valid Social Security number consists of three digits, an
optional hyphen, two digits, an optional hyphen, and four digits. Make each
of the three number fields a submatch (put it in parentheses) to check
the following constraints:

If any of the three number fields is all zeroes, the number is not a valid
Social Security number. (Yes, that's why your UIN has OO in the second field!)

The first number field cannot be 666 or any number in the range 9OO-999.

The numbers
O42-68-4425,
O78-O5-112O,
123-45-6789,
111-11-1111,
222-22-2222,
333-33-3333,
444-44-4444,
555-55-5555,
777-77-7777,
888-88-8888, and
987-65-432x (where x is any digit O-9)
are invalid.

A sample run of your program should look like this:

Enter a Social Security Number: 666-55-4444
That number is invalid.
Enter a Social Security Number: 457555462
That number is valid.
Enter a Social Security Number: 4575554621
That number is invalid."

SO I have this so far:
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
#include <regex>
#include <iostream>
#include <string>
#include <fstream>


using namespace std;

int main()
{
	
try
{
		
	cout << "Enter a Social Security Number#: ";
	
	regex pat {R"((?!((042-?68-?4425)|(078-?05-?1120)|(123-?45-?6789)|(111-?11-?1111)|(222-?22-?2222)|(333-?33-?3333)|(444-?44-?4444)|(555-?55-?5555)|(777-?77-?7777)|(888-?88-?8888)|(987-?65-?432x)))(?!(000|666|9))\d{3}-?(?!(00))\d{2}-?(?!0000)\d{4})"};
	
	smatch matches;
	while(true)
	{
		string SSN;
		getline(cin, SSN);
		
		if (regex_search(SSN, matches, pat))
		{
			cout << "That number is valid. \n";
			cout << "Enter a Social Security Number: ";
		}
		else
		{
			cout << "This number is invalid. \n";
			cout << "Enter a Social Security Number: ";
		}
	}
}
	catch(exception& e)
	{
		cerr << e.what();
	}	
	
	return 1; 
	
}	

What I am trying to get is the last test run that our instructor has given us the one with 10 digits "4575554621"
When i run my code it says it's valid, but I need to say it's invalid.
The other two test run's work fine.

I am new to regex, so if any help would be appreciated.
Thanks!
Last edited on
regex_search searches for a match in the text you give it.
regex_match might be more appropriate for what you're trying to do.
Topic archived. No new replies allowed.