Nested Loop Help

So, I'm trying to write a loop that will verify that a user has not entered "Q" initially, then I need it to verify whether the first three characters in a string are letters and if they aren't, it needs to reprompt and repeat. Once that is verified I need it to verify that the last three letters are all numbers and then I need it to verify that there are only 6 characters.

My code works until a single loop has run through, because I just have on ebig nested loop it isn't going back to the beginning to verify the other requirements. So.. What would be the best way to make this work?

Here is my piece of code for this 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
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
cout << endl << "Enter a LICENSE PLATE (or type Q to quit the program):" << endl;
			
cin >> inputLicense;
			
cout << endl;
						
while(inputLicense != "Q" && inputLicense != "q")
{
	while( count <= 2 )
	{
		if( !isalpha(inputLicense[count]) )
		{
			cout << "License " << inputLicense << " is formatted incorrectly." << endl;
			cout << "Must be 3 letters followed by 3 digits.  Try again." << endl << endl;
			
			cout << endl << "Enter a LICENSE PLATE (or type Q to quit the program):" << endl;
		
			cin >> inputLicense;
			
			cout << endl;
			
			count = 0;
		}
		else
			count++;
		}	
	
	while( count <= 5 && count > 2 )
	{
		if( !isdigit(inputLicense[count]) )
		{
			cout << "License " << inputLicense << " is formatted incorrectly." << endl;
			cout << "Must be 3 letters followed by 3 digits.  Try again." << endl << endl;
			
			cout << endl << "Enter a LICENSE PLATE (or type Q to quit the program):" << endl;
		
			cin >> inputLicense;
			
			cout << endl;
			
			count = 0;
		}
		else
			count++;
	}
				
	while( count > 5 )
	{
		cout << "License " << inputLicense << " is too long." << endl;
		cout << "Must be 3 letters followed by 3 digits.  Try again." << endl << endl;
		
		cout << endl << "Enter a LICENSE PLATE (or type Q to quit the program):" << endl;
	
		cin >> inputLicense;
		
		cout << endl;
		
		count = 0;	
	}
				
	cout << "Looks Good." << endl << endl;
	cout << endl << "Enter a LICENSE PLATE (or type Q to quit the program):" << endl;
		
	cin >> inputLicense;
			
	cout << endl;
	
	count = 0;
}
			
cout << "End of Program.";
I tried to change the stuff a little bit here is my attempt.

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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include<iostream>
#include<string>

using namespace std;

int main()
{

// get everything at once here.
string inputLicense;
getline(cin, inputLicense);

// keep repeating until you find a q at the beginning
while(&inputLicense[0] != "Q" || (&inputLicense[0] != "q"))
{
         // display the string
	cout << inputLicense.c_str();
        
        // if it first character is q quit the program
	if((inputLicense[0] == 'Q') || (inputLicense[0] == 'q')) 
	{
		cout  << "quitting" << endl;
		return 0;
	}
	else if(inputLicense.size() > 6 )    // if greater than 6 characters
	{
		cout <<  endl << "License too long" << endl;
	}
	else if(inputLicense.size() == 6) // if it is equal to 6 characters
	{
	
	// verify its a letter
	int i = 0;
	string letter3 = inputLicense.substr(0,3); 
	while (letter3[i])
	{
		if (isalpha(letter3[i])) 
		{
			cout << "\ncharacter " << letter3[i] << " is alphabetic\n";
		}
		else 
		{
			cout << "\ncharacter " << letter3[i] << "is not alphabetic\n";
		}
		i++;
	}

	// verify its numeric
	string number3 = inputLicense.substr(3,3);
	i = 0;
	while (number3[i])
	{
		if (isdigit(number3[i])) 
		{
			cout << "\ncharacter " << number3[i] << " is numeric\n";
		}
		else 
		{
			cout << "\ncharacter " << number3[i] << "is not numeric\n";
		}
		i++;
	}

	}
	else
	{
		cout << "can't have less than 6 characters " << endl;
	}	

	getline(cin, inputLicense);	
}

	int x;
	cin >> x;
	return 0;
}


i didn't complete the program but hopefully you get the idea.
Have a read of this :

http://www.cplusplus.com/forum/beginner/104553/2/#msg564228


When using chars instead of ints, make use of the toupper function to make comparisons easier, that is avoid having to compare to 'Q' and 'q', just 'Q' only.

Hopefully you will find this code more tidy.

HTH
Topic archived. No new replies allowed.