Switch structure using char

my switch structure keeps exiting and I don't know why.. I had it working great until I switched from int to char in order to add case Q.

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

*********************/

#include <iostream>
#include <iomanip>
#include <conio.h>



int main ()
{
	//set variables


double hrlyWage;
double mgrWage;
double commWage;
double widgetWage;
double hrlyPay;
double commPay;
double widgPay;
double hours;
double commPercent;
double widgets;
double commSales;

	

double totalPay = 0;
char paycode;


//display menu for user selection


std::cout << "Enter Pay code (or [Q]uit): " << std::endl;
std::cout << "[1] Manager " << std::endl;
std::cout << "[2] Hourly Worker " << std::endl;
std::cout << "[3] Commission Worker " << std::endl;
std::cout << "[4] Widget Worker " << std::endl;
std::cin >> paycode;

switch (toupper(paycode))
{
case '1':
		std::cout << "Manager selected.\n" << "Enter weekly salary: ";
		std::cin >> mgrWage;
		std::cout << "Manager's pay is $" << std::setprecision(2) << std::fixed << mgrWage << std::endl;
		totalPay += mgrWage;
		break;

case '2':
		std::cout << "Hourly worker selected." << std::endl;
		std::cout << "Enter hourly pay: ";
		std::cin >> hrlyWage;
		std::cout << " Enter total hours Worked: ";
		std::cin >> hours;
		hrlyPay = (hrlyWage * hours);
		totalPay += hrlyPay;
		std::cout << "Total hourly worker's pay is: $" << std::setprecision(2) << std::fixed << hrlyPay;

		break;
case '3':

		std::cout << "Commission worker selected." << std::endl;
		std::cout << "Enter weekly salary: ";
		std::cin >> commWage;
		std::cout << "Enter commission (%): ";
		std::cin >> commPercent;
		std::cout << "Enter gross weekly sales";
		std::cin >> commSales;
		commPay = ((commPercent * .01) * commSales) + commWage;
		totalPay += commPay;
		std::cout << "Commission worker's pay is $ " << std::setprecision(2) << std::fixed << commPay;
		break;
	
case '4':
		std::cout << "Widget worker selected." << std::endl;
		std::cout << "Enter pay per widget: ";
		std::cin >> widgetWage;
		std::cout << "Enter number of widgets: ";
		std::cin >> widgets;
		widgPay = widgetWage * widgets;
		totalPay += widgPay;
		std::cout << "Widget worker's pay is: $" << std::setprecision(2) << std::fixed << widgPay << std::endl;
		break;
	
case 'Q':

	std::cout << "Total Pay is: $" << std::setprecision(2) << totalPay << std::endl;
	std::cout << "Press ENTER";
	getch();
}
}
//display total salary
std::cout << std::endl;
std::cout << "Total Pay is: $" << std::setprecision(2) << totalPay << std::endl;
}
Last edited on
closed account (SECMoG1T)
your program should run only once and exit. maybe if you need iteration you might consider adding the swicth into a loop or i din't get the question clearly. the section below
//display total salary is out of main too.
Last edited on
the only think I can find really wrong here is the double } before the final display of salary. other than that, it compiles and runs fine (a few minor i/o formatting quibbles aside).
Last edited on
whichever case I enter, it only runs that particular case and exits completely. it doesn't allow me to make another selection
You need a loop of some kind.
http://www.cplusplus.com/doc/tutorial/control/
closed account (SECMoG1T)
whichever case I enter, it only runs that particular case and exits completely
the reason being that after the break statement is executed control is returned back to
main which executes the rest of the code after the switch. if you can probably plug
that switch into a loop, control from break will be returned back to the loop which will evaluate some condition and decide to run the loop again or return control to main. my
understanding of switch statements is that they are similar to if,else statements though they
provide their own unique dynamicity in programs.

1
2
3
4
5
6
7
8
while(condition)
 {
    get input;
    switch(input)
     {
         ///cases
     }
 {
Last edited on
now when I add the while condition it is giving me the error "uninitialized local variable 'paycode' used.

You're probably comparing your paycode variable tos something in your while condition, before you do std::cin >> payload for the first time.
closed account (SECMoG1T)
please post your code you'r working with so we may help
Topic archived. No new replies allowed.