Having Trouble With the Switch Command

I'm only 9 chapters into the dummies guide to C++ and I am completely new to the programming world. I'm learning on my own and I'm seeking help understanding what I am doing wrong. I have spent time re-reading the chapters I've covered so far and have gone further to write my own programs using the course material that get's covered and have encountered a problem that I would very much like help with. Under the if(nState == 4) I use a switch statement to allow for the user to input the abbreviation of any of the 50 states. It follows with defined case commands for all the variable which work. Though, the nTaxCode will not accept the value as defined nTaxCode = 1; to carry that value after the break to complete the program. Am, I a, not able to carry the value with the switch and case statements beyond the case statements, or am I writing the case statement for what nTaxCode will be??

Thank you in advance for any help you can offer.

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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
	if (nSelection == 4)
		{
			int nTaxCode;
			cout << "Welcome to the Tax Collector\n"
				<< "\n"
				<< "Please enter the state you wish\n"
				<< "to calculate the price with tax,\n"
				<< "by entering the two letter state\n"
				<< "abbreviation.\n"
				<< "\n"
				<< "Than enter the price of the item\n"
				<< "you wish to know the tax of.\n"
				<< "\n"
				<< "\n"
				<< "Please Enter The State: ";
			cin >> nState;

			switch (nState)
			{
			case 'al':
			case 'AL':
			case 'Al':
			case 'ga':
			case 'Ga':
			case 'GA':
			case 'hi':
			case 'Hi':
			case 'HI':
			case 'la':
			case 'La':
			case 'LA':
			case 'ny':
			case 'Ny':
			case 'NY':
			case 'sd':
			case 'Sd':
			case 'SD':
			case 'wy':
			case 'Wy':
			case 'WY':
			case 'mo':
			case 'Mo':
			case 'MO':
				cout << "6% sales tax";
				nTaxCode = 6;
				break;
			case 'ak':
			case 'Ak':
			case 'AK':
			case 'de':
			case 'De':
			case 'DE':
			case 'mt':
			case 'Mt':
			case 'MT':
			case 'nh':
			case 'Nh':
			case 'NH':
			case 'or':
			case 'Or':
			case 'OR':
				cout << "There is no Sales TAX in selected State."
					<< "\n";
				break;
			case 'az':
			case 'Az':
			case 'AZ':
			case 'fl':
			case 'Fl':
			case 'FL':
			case 'ct':
			case 'Ct':
			case 'CT':
			case 'id':
			case 'Id':
			case 'ID':
			case 'il':
			case 'Il':
			case 'IL':
			case 'ia':
			case 'Ia':
			case 'IA':
			case 'ky':
			case 'Ky':
			case 'KY':
			case 'me':
			case 'Me':
			case 'ME':
			case 'md':
			case 'Md':
			case 'MD':
			case 'ma':
			case 'Ma':
			case 'MA':
			case 'mi':
			case 'Mi':
			case 'MI':
			case 'ne':
			case 'Ne':
			case 'NE':
			case 'oh':
			case 'Oh':
			case 'OH':
			case 'pa':
			case 'Pa':
			case 'PA':
			case 'sc':
			case 'Sc':
			case 'SC':
			case 'vt':
			case 'Vt':
			case 'VT':
			case 'wv':
			case 'Wv':
			case 'WV':
				cout << "4% sales tax";
				nTaxCode = 4;
				break;
			case 'co':
			case 'Co':
			case 'CO':
				cout << "7% Sales Tax";
				nTaxCode = 3;
				break;
			case 'ar':
			case 'Ar':
			case 'AR':
			case 'in':
			case 'In':
			case 'IN':
			case 'ks':
			case 'Ks':
			case 'KS':
			case 'mn':
			case 'Mn':
			case 'MN':
			case 'ms':
			case 'Ms':
			case 'MS':
			case 'nv':
			case 'Nv':
			case 'NV':
			case 'nj':
			case 'Nj':
			case 'NJ':
			case 'ri':
			case 'Ri':
			case 'RI':
			case 'tn':
			case 'Tn':
			case 'TN':
			case 'wa':
			case 'Wa':
			case 'WA':
				cout << "5% sales tax";
			    nTaxCode = 4;
				break;
			case 'nd':
			case 'Nd':
			case 'ND':
			case 'ut':
			case 'Ut':
			case 'UT':
			case 'ok':
			case 'Ok':
			case 'OK':
			case 'nm':
			case 'Nm':
			case 'NM':
			case 'nc':
			case 'Nc':
			case 'NC':
			case 'va':
			case 'Va':
			case 'VA':
			case 'wi':
			case 'Wi':
			case 'WI':
				cout << "3% sales tax";
				nTaxCode = 5;
				break;
			case 'ca':
			case 'Ca':
			case 'CA':
			case 'tx':
			case 'Tx':
			case 'TX':
				cout << "8% sales tax";
				nTaxCode = 6;
				break;
			}
			cout << "\n"
				<< "Please enter amount of Item: ";
			cin >> nAmount;
			int nValue1;
			int nValue2;

			nValue1 = nAmount * nTaxCode;
			nValue2 = nAmount * (nTaxCode + 1);

			cout << "\n";
			cout << "Price of item: " << nAmount
				<< "\n"
				<< "Sales Tax    : " << nValue1
				<< "\n"
				<< "Total        : " << nValue2
				<< "\n";

		}
	
		if (nSelection == 0)
		{
			cout << "\n"
				<< " Now Exiting Program.";
		}
	system("PAUSE");
	return 0;
}
Last edited on
1- Compile with warnings
2- Read the warnings
See now, that's the problem the compiler does not report any errors. Yet when I run the program and activate the nState switch and enter the start abbreviation I receive a Run- Time Check Failure#3 - T. I wrote a switch command earlier in the code without issue. Though I wasn't trying to apply a value to an int.
When I debug the program in visual studios it tells me "The variable 'nTaxCode' is being used without being initialized." Which only informs me of my initial question. Why when I use switch and case does it not accept the value entered for nTaxCode after the break;?
Last edited on
> the compiler does not report any errors no, they are warnings.
No, they are warnings.
That's why you need to enable the warnings when compiling.

> does it not accept the value entered for nTaxCode
it never sets any value because all the cases fail.
'al' is not a character. And I wonder about nState

(Also in 'ak' you are not giving it any value)
Thank you but my warnings are set to show and none show. No errors, no warnings, no issues with the build until it runs. ak and al are just abbreviations of states as the program will eventually calculate taxes for the state entered. nState is written "char nState;" which was added in the earlier set of the code which does not currently show. When a user selects option 4 it prompts them to enter their state abbreviation. The case as I understand is then written for every instance a user can input the abbreviation. Followed then by the cammand if they do which I'm attempting to then give nTaxCode a value with "nTaxCode = 1" or 2, or 3, or so on. Though nTaxCode does not initialize the requested value for the continuation of the program after the break;.
Sure nState is of type char, but non of the cases are of type char. char is a single character, like a or b or c. Not 2 characters like al, AL and Al. So non of the cases will ever be true.
Last edited on
Well that makes sense. Thank you very much. I will have to review the book again, though I do not recall it stating such as it should. Though, again it may go into more detail on the subject in later chapters. I've been running laps trying to rewrite my code to make it function. Seems that this part cannot function at all and will need to be rewritten. Still though, thank you very much.
Topic archived. No new replies allowed.