Exiting the program after inputting 'X'?

Hello,

I'm trying to work out all the bugs from my program, one of them is that my program will not exit if the user inputs X or x. Thank you.

Here is my program:
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
  //***************************************************************************
string userInput()
{
	bool goodID = false;
	string id = " ";
	
	cout << endl << endl;
	cout << "Enter ID number (or enter X to end): " << endl;
    cin  >> id;
    
    while (id != "X"  && !goodID)
    {
        if (testValidity(id) == true)
        {
        	cout << endl;
            cout << "INVALID ID FORMAT" << endl;
            cout << "Must start with two characters (A to Z)and end with six digits" << endl;
            cout << "Enter ID number (or enter X to end): " << endl;
	    	cin  >> id;
        }
        else
        {
        	cout << endl;
            cout << "VALID ID FORMAT" << endl;
            goodID = true;
        }    
    }
    return id;
}
//***************************************************************************
bool testValidity(string id)
{
    bool invalidLetter = false;
    for (unsigned int i= 0; i < id.length()-1; i++)
    {
        if (i > 1 && isalpha(id[i]))
            invalidLetter = true;
        if (i < 2 && isdigit(id[i]))
            invalidLetter = true;
    }
    if (id.length()!= 8)
    	invalidLetter = true;
    	
    return invalidLetter;
}

My ID number looks like AS123456. That's a valid ID. It should exit when you enter X or x. Thanks again.
Last edited on

Did you actually try shift key x ?
I changed line 11 above and it seemed to work ok?

while ( id != "X" && id !="x" && !goodID)

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
#include <iostream>

using namespace std;

bool testValidity(string id)
{
    bool invalidLetter = false;
    for (unsigned int i= 0; i < id.length()-1; i++)
    {
        if (i > 1 && isalpha(id[i]))
            invalidLetter = true;
        if (i < 2 && isdigit(id[i]))
            invalidLetter = true;
    }
    if (id.length()!= 8)
    	invalidLetter = true;

    return invalidLetter;
}

string userInput()
{
	bool goodID = false;
	string id = " ";

	cout << endl << endl;
	cout << "Enter ID number (or enter X to end): " << endl;
    cin  >> id;



    while ( id != "X" && id !="x" && !goodID)
    {
        if (testValidity(id) == true)
        {
        	cout << endl;
            cout << "INVALID ID FORMAT" << endl;
            cout << "Must start with two characters (A to Z)and end with six digits" << endl;
            cout << "Enter ID number (or enter X to end): " << endl;
	    	cin  >> id;
        }
        else
        {
        	cout << endl;
            cout << "VALID ID FORMAT" << endl;
            goodID = true;
        }
    }
    return id;
}
//***************************************************************************


int main()
{
    cout << userInput() << endl;
    return 0;
}

When I added the while loop inside my main, it all worked out. Thank you for your response.
Topic archived. No new replies allowed.