using both int and char types for cases in one switch statement

Hi all,

I'm new (to both the forum and the language), and I'm at an impasse with an assignment. I wonder if someone would point me in the right direction. I've checked around the site (including the control tutorial and a search of the beginner's forum for "switch") and net and haven't found a completely satisfactory solution. This is my first C++ assignment ever. (Mercy, please!)

I'm required to use Visual Studio 2005, and my program is compiling with no errors. I've bolded the areas in question (to the best of my ability.)

I have a switch statement inside of a do-while loop. (I don't think the do-while loop is working properly either, but one problem at a time.) When I select case x, the program is supposed to display the totals for each category of expense claims. However, when I select x, I get my default case instead. I wonder if the program is implicitly casting x as an int for me or if I need to explicitly cast it somehow to make it work correctly. At one point, the compiler told me that x needs to be a constant, which is why I tried declaring it as such. My brain is fried at this point from staring at the code, and I've run out of ideas to tinker with. Can I use both int's and char's in one switch statement? If not, any thoughts on how to proceed? I'm required to use x as the display totals case. Am I at least on the right track?

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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
int main ()
{
	int choice;		// stores selection value
	float expenseAmount = 0.0; // stores expense amount input
	float car = 0.0; // Car Expense
	float util = 0.0; // Utility Expense
	float rent = 0.0; // Rent Expense
	float cap = 0.0; //Capital Expense
	float meal = 0.0; // Meals and Entertainment
	float it = 0.0; // IT Consulting Fees
	const int x = 0; // Display Totals
	char indicator = 'y'; 

	do
	{
	system("cls");

	cout << "Please choose from the following expense types: "
		 << endl << "1\tCar Expense (2150)"
		 << endl << "2\tUtility Expense (2250)"
		 << endl << "3\tRent Expense (1500)"
		 << endl << "4\tCapital Expense (2200)"
		 << endl << "5\tMeals and Entertainment (8750)"
		 << endl << "6\tIT Consulting Fees (5874)"
		 << endl << "x\tExit Menu/Display Totals"
		 << endl;

	cin >> choice;		// collect choice from user

	switch (choice)		// process user's choice
	{															
	case 1: system("cls");
			cout << "Enter amount" << endl;
				 cin >> expenseAmount;	
				 if (expenseAmount <= 0.0)
				 {
				 cout << "Please enter a valid amount." << endl;
				 }
				 else
				 {
					 car += expenseAmount;
				 }
			     break;
	case 2: system("cls");
			cout << "Enter amount" << endl;
				 cin >> expenseAmount;
				 if (expenseAmount <= 0.0)
				 {
				 cout << "Please enter a valid amount." << endl;
				 }
				 else
				 {
					 util += expenseAmount;
				 }
			     break;
	case 3: system("cls");
			cout << "Enter amount" << endl;
				 cin >> expenseAmount;
				 if (expenseAmount <= 0.0)
				 {
				 cout << "Please enter a valid amount." << endl;
				 }
				 else
				 {
					 rent += expenseAmount;
				 }
			     break;
	case 4: system("cls");
			cout << "Enter amount" << endl;
				 cin >> expenseAmount;
				 if (expenseAmount <= 0.0)
				 {
				 cout << "Please enter a valid amount." << endl;
				 }
				 else
				 {
					 cap += expenseAmount;
				 }
			     break;
	case 5: system("cls");
			cout << "Enter amount" << endl;
				 cin >> expenseAmount;
				 if (expenseAmount <= 0.0)
				 {
				 cout << "Please enter a valid amount." << endl;
				 }
				 else
				 {
					 meal += expenseAmount;
				 }
			     break;
	case 6: system("cls");
			cout << "Enter amount" << endl;
				 cin >> expenseAmount;
				 if (expenseAmount <= 0.0)
				 {
				 cout << "Please enter a valid amount." << endl;
				 }
				 else
				 {
					 it += expenseAmount;
				 }
			     break;
	case x:  system("cls");
		cout << "General Ledger Totals:" << endl;
	        cout << "Total Car Expenses (2150): " << car << endl;
	        cout << "Total Utility Expenses (2250): " << util << endl;
		cout << "Total Rent Expenses (1500): " << rent << endl;
		cout << "Total Capital Expenses (2200): " << cap << endl;
		cout << "Total Meals and Entertainment (8750): " << meal << endl;
	        cout << "Total IT Consulting Fees (5874): " << it << endl;
		cout << endl;
			   break;
	default: cout << endl << "You entered an invalid character. Try again."
				  << endl;
	} while ((indicator == 'y') || (indicator == 'Y'));
        
        return 0;
}

Whoops!

I forgot to add that when I select x, I get the default statement, but the console window flickers, like it's in an infinite loop. Not sure if that matters.
Last edited on
Your variable 'choice' is an int, and thus can only hold an integral value. The letter 'x' is not an integral value, so that's inappropriate for this situation.

Since the user is inputting a character, you'll want to use char for input. You'll then want to compare it with the characters '0', '1', 'x', etc. Not the numbers 0, 1, etc

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
char choice;

//...

cin >> choice;

//...

switch(choice)
{
case '0':
   ...

case '1':
   ...

case 'x':
   ...
};


Note the numbers inside of 'single quotes'.


EDIT:

Also:

Thank you for posting in an intelligent, comprehendable, and well formatted manner. I can't tell you how many people come to these boards and post ambiguous questions and slap their code in without any explanation/formatting/etc.

Seeing posts like this from newcomers is a nice breath of fresh air. We need newbies to use this post as an example of how to ask a question the right way.
Last edited on
That did it! Many thanks to you, Disch! It's so easy to lose perspective and have tunnel-vision sometimes. Now off to fix everything else...

Thank you for posting in an intelligent, comprehendable, and well formatted manner. I can't tell you how many people come to these boards and post ambiguous questions and slap their code in without any explanation/formatting/etc.

Seeing posts like this from newcomers is a nice breath of fresh air. We need newbies to use this post as an example of how to ask a question the right way.


I'm not sure what to say, except you're welcome and thank you. Communication: It's what's for dinner. :~)
Topic archived. No new replies allowed.