First Loop-Based Program: Undeclared Identifier?

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
#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
	int number = 0;
	float cost = 0.0;
	char beverage;
	
	bool validBeverage;

	cout << fixed << showpoint << setprecision(2);
	
	do
	{
		cout << endl << endl;
		cout << "Hot Beverage Menu" << endl << endl;
		cout << "A: Coffee         $1.00" << endl;
		cout << "B: Tea            $ .75" << endl;
		cout << "C: Hot Chocolate  $1.25" << endl;
		cout << "D: Cappuccino     $2.50" << endl <<endl << endl;
		
		cout << "Enter the beverage A,B,C, or D you desire" << endl;
		cout << "Enter E to exit the program" << endl << endl;
		cin >> beverage;
		
		switch(beverage)
		{
		case 'a':
		case 'A':
		case 'b':
		case 'B':
		case 'c':
		case 'C':
		case 'd':
		case 'D':  validBeverage = true;
				   break;
		default:   validBeverage = false;
		}

		if (validBeverage == true)
		{
			cout << "How many cups would you like?" << endl;
			cin >> number;
		}
		switch (beverage)
		{
		case 'a':
		case 'A': cost = number * 1.0;
			    cout << "The total cost is $ " << cost << endl;
				break;
		case 'b':
		case 'B': cost = number * .75;
				cout << "The total cost is $ " << cost << endl;
				break;
		case 'c':
		case 'C': cost = number * 1.25;
				cout << "The total cost is $ " << cost << endl;
				break;
		case 'd':
		case 'D': cost = number * 2.5;
				cout << "The total cost is $ " << cost << endl;
				break;
		case 'e':
		case 'E': cout << "Please come again." << endl;
				break;
		default:cout << " The selection you made is invalid." << endl;
				cout << " Try again please" << endl;
		}

	} while (beverage != e || beverage != E);

	return 0;
}


Hello all! I am writing my first loop-based program and have run into just one difficulty in getting it to compile. Whenever I go to run this program, I receive the following set of errors:

1>f:\documents and settings\kevin\desktop\programming .cpp files\lab 5\lab 5\lab 51.cpp(79): error C2065: 'e' : undeclared identifier
1>f:\documents and settings\kevin\desktop\programming .cpp files\lab 5\lab 5\lab 51.cpp(79): error C2065: 'E' : undeclared identifier

If aynone can help, please advise me on what steps I can take to get this program to function. Thanks in advance for your assistance.
- Kevin
closed account (S6k9GNh0)
while (beverage != e || beverage != E);

e or E do not exist and these aren't being used as literal characters.

Fix:
while (beverage != 'e' || beverage != 'E');

I would also initialize beverage to something to make sure its not junk. If I'm not mistaken, cin doesn't always give the variable a value.
Thank you. That fixes the error of an undeclared identifier, but the program still does not exit after the user selects 'e' or 'E'. Is there something I am missing about the while() line?
This might simplify the whole thing.

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
#include <iostream>
using namespace std;

int main()
{
	int number = 0;
	float cost = 0.0;
	char beverage ='n';
	
do
{		
		cout << "\n\nHot Beverage Menu.\n\n";
		cout << "A: Coffee         $1.00\n";
		cout << "B: Tea            $ .75\n";
		cout << "C: Hot Chocolate  $1.25\n";
		cout << "D: Cappuccino     $2.50\n\n\n";
		
		cout << "Enter A,B,C, or D." << endl;		
		cout << "Enter E to exit the program" << endl << endl;
		
		cin>> beverage;
		beverage = toupper(beverage);   //makes input all upper-case (makes it a bit easier in switch statements)
		
		if (beverage != 'E')
			{
				cout << "How many cups would you like?" << endl;
				 cin>>number;
		    }
				
		switch (beverage)
		{
			
		case 'A': cost = number * 1.0;
			      cout << "The total cost is $ " << cost << endl;
				  break;

		case 'B': cost = number * .75;
				  cout << "The total cost is $ " << cost << endl;
				  break;

		case 'C': cost = number * 1.25;
				  cout << "The total cost is $ " << cost << endl;
				  break;

		case 'D': cost = number * 2.5;
				  cout << "The total cost is $ " << cost << endl;
				  break;
					
		case 'E': break; 
			
		default:  cout << " The selection you made is invalid." << endl;
				  cout << " Try again please" << endl;
				
			
		}
		cout << "Thank-you. Please come again. Press 'enter'" << endl;
		cin.ignore().get();
		
}while ((beverage != 'A')||(beverage != 'B')||(beverage != 'C')||(beverage != 'D')||(beverage != 'E'));

	return 0;
}
Last edited on
Topic archived. No new replies allowed.