string help

I have a customer code that should follow the format of four letters followed by four numbers (i.e. JEFF7465, QUIN2983, etc) I need to validate that the customer code entered by the user is 4 letters followed by 4 numbers...I haven't quite understood how to get there.
I have been able to error check the length of the string:
1
2
3
4
5
6
7
8
9
10
	// if customer enters E or e, exit the program
	if (userCustomerCode == "E" || userCustomerCode == "e")
	{
		return 1;
	}
	// if the length of the customer code is not 8 "characters"
	else if (userCustomerCode.length() != 8)
	{
		cout << "ERROR" << endl;
	}


but i am unsure how to check the format. Can someone lend a hand or a push in the right direction?
This sounds like a job for regex (assuming you've got an up-to-date compiler). I am not good with regex, so I stand by to be corrected. There is a more elegant way to do it, but this should be reasonably easy to understand.

1
2
3
4
5
6
regex r("[a-zA-Z][a-zA-Z][a-zA-Z][a-zA-Z][0-9][0-9][0-9][0-9]");

if(regex_match(userCustomerCode ,r))
			cout<<"This input does contain four characters and four numbers"<<endl;

// Don't forget to test for length eight 

Last edited on
I have never seen this before. I may have to do a little researching to figure out what this means. I am using Visual C++ 2010 express.
Regular expressions are essentially a very popular means of pattern matching.

regex r("[a-zA-Z][a-zA-Z][a-zA-Z][a-zA-Z][0-9][0-9][0-9][0-9]");
creates a regex object with a pattern of
[any letter][any letter][any letter][any letter][any digit][any digit][any digit][any digit]
and this
regex_match(userCustomerCode ,r)
simply checks the pattern against a provided string.

Regular expressions are astonishingly powerful, and available everywhere that a *nix hacker has been let loose :p Learning the basics of them will be time well spent.
I see what it is doing now. Only thing I see differently is that is should be:
1
2
3
4
regex r("[a-zA-Z][a-zA-Z][a-zA-Z][a-zA-Z][0-9][0-9][0-9][0-9]");

if(!regex_match(userCustomerCode ,r))
			cout<<"This input does contain four characters and four numbers"<<endl;


Since I am error checking if it DOES NOT match...correct?
Now that I have played around with it a bit, I am back to square one. I don't think regex is the way to do this. I still need to be able to exit the program with "E" or "e" at the same prompt. So if the user enters the e...it gives the same error about not being formatted, rather than exiting the program. I tried this with a while loop:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
do
	{
		regex r ("[a-zA-Z][a-zA-Z][a-zA-Z][a-zA-Z][0-9][0-9][0-9][0-9]");

		cout << "Enter customer code (or E to exit): ";
		cin >> userCustomerCode;
		//getline (cin, userCustomerCode);

		cout << endl;

		// if the customer code does not follow format of 4 letters then 4 numbers
		if (!regex_match (userCustomerCode, r))
		{
			cout << "Invalid Entry! Customer code " << userCustomerCode << " is formatted incorrectly." << endl;
			cout << "Must be 4 letters followed by 4 digits. Try again." << endl;
		}
		// if the length of the customer code is not 8 "characters"
		else if (userCustomerCode.length() != maxStringSize)
		{
			cout << "Invalid Entry! Customer code " << userCustomerCode << " is too long." << endl;
			cout << "Must be 4 letters followed by 4 digits. Try again." << endl;
		}
	}
	while (userCustomerCode != "E" || userCustomerCode != "e");
while (userCustomerCode != "E" || userCustomerCode != "e")

Can you think of a value for userCustomerCode for which this will not be true (and thus the loop will stop)? I can't. This loop will loop forever.
..... && .... instead of ||
I'm not sure i understand. It still does not work.

I am trying to say...to prompt user for userCustomerCode and check for valid entry, while the user does not enter E or e ( and if they do, it should exit the program)
Topic archived. No new replies allowed.