char sentinel value in a switch structure!

Since this is an ongoing project, I am only going to post the beginning of my code. I have a switch structure which the user loops through with an int value. However, the sentinel value must be the char Q. Everytime I try to put in the sentinel value it is giving me errors, should it be before the main structure? or within it?

I was able to do a const char sentinel = Q, but hitting Q simply caused a never ending loop

needless to stay I am stuck! simply the char vs int value has me caught up!

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
  /*********************


Payroll program to determine various employees pay and total company pay
*********************/

#include <iostream>
#include <iomanip>


int main ()
{
	//set variables



double totalPay = 0;
int paycode = 0;
double counter = 0;
while (paycode <= 4)
	
{
//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 (paycode)
{
case 1:
	{
		std::cout << "Manager selected.\n" << "Enter weekly salary: " << std::endl;
		std::cin >> mgrWage;
		std::cout << "Manager's pay is $" << std::setprecision(2) << std::fixed << mgrWage << std::endl;
		totalPay += mgrWage;
	break;
	}
case 2:
	

}
}
Last edited on
I'm not fully sure of what exact error that you are getting, but the issue currently is that your quit command requires you to store a char in an int which will cause unwanted behavior. My best advice would be to simply store the user's input as a char. That way you can utilize both ints and chars.
1
2
3
4
5
6
7
8
9
10
11
12
char paycode;
std::cin >> paycode;

switch (paycode)
{
    case '1':
    //manager stuff
    break;
    case 'Q':
    std::cout << "Exiting...\n";
    break;
}
Topic archived. No new replies allowed.