string to character??

I cannot for the life of me figure out how to end the loop if the customer enters 'E' or 'e'. I have to use a string for the userCustomerCode, so how do i check for a character?? HELP!!!

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
void UserInteraction ()
{
	// local constant
	int maxStringSize = 8;
	int maxTempSize = 3;

	// local variables
	string userCustomerCode;
	string tempA;
	string tempB;

	do
	{
		cout << "Enter customer code (or E to exit): ";
		cin >> userCustomerCode;

		cout << endl;

		tempA = userCustomerCode.substr (0, 4);
		tempB = userCustomerCode.substr (4, 4);

		for (int i = 0; i < tempA.length (); i++)
		{
			tempA [i] = toupper (tempA [i]);
		}

		cout << tempA << endl;
		cout << tempB << endl;

		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;
		}
		else if (!isalpha (tempA [maxTempSize]))
		{
			cout << "Invalid Entry! Customer Code " << userCustomerCode << " is formatted incorrectly." << endl;
			cout << "Must be 4 letters followed by 4 digits. Try again." << endl;
		}
		
		for (int i = 0; i < tempB.length (); i++)
		{
			if (!isdigit (tempB [i]))
			{
				cout << "Invalid Entry! Customer Code " << userCustomerCode << " is formatted incorrectly." << endl;
				cout << "Must be 4 letters followed by 4 digits. Try again." << endl;
			}
		}
	}
	while (!userCustomerCode.find ("E       ") || !userCustomerCode.find ("e       "));
}
I think that is better to check if that string variable has got the 'E' character after it is read. In that way, you will not execute the whole loop until the while-condition sees the stop-flag.
I think you can do that much easier, just by making an equality check: if(useCustomerCode !="E". I did't check that, to see if it's correct, but I'm just saying.
closed account (j2NvC542)
A string consists of characters and can be accessed like an array (with the [ ] parentheses) or with the string member function at().
For example if you want to get the first character of a string, you write userCustomerCode[0]; // 0 because the first element is always 0 . Or userCustomerCode.at(0);
The difference between the access operator [] and the member function at() is that if you try to read an element which doesn't exist (is out of boundaries), the member function will throw an exception (error) while the operator won't.
Last edited on
You can simplify your task if you will write one more function.

1
2
3
4
5
6
bool get_input( std::string &s )
{
   std::cout << "Enter a customer code (or E to exit): ";

   return ( std::cin >> s && s != "E" && s != "e" );
}


Then you can use it in your function UserInteraction the following way

Instead of

1
2
3
4
5
6
7
do
{
   cout << "Enter customer code (or E to exit): ";
   cin >> userCustomerCode;
// ...
}  while (!userCustomerCode.find ("E       ") || !userCustomerCode.find ("e       "));
}


the following shall be

1
2
3
4
while ( get_input( userCustomerCode ) )
{
//...
}
Last edited on
@CosminNTG - I tried that, but every time I entered an E or e to test my code, the program quit with an unexpected error...I tried with "E" and 'E' and "e" and 'e'.
Topic archived. No new replies allowed.