Trouble with if statement within a switch case.

I am trying to write a looping switch case. You have four separate payroll options, each with their own steps. Once you are done with one person's payroll the program loops and you move onto the next. Almost everything seems perfect except that I am supposed to have a separate payment for workers who worked over 40 hours. I wrote a if else statement but it only gives the base pay. The if else is on lines 37-48
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
  #include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
int main()
{
	int ans;     // For the paycode
	double amount, salary, hours, weeklysales, pieces, priceperpiece;
	
	cout << "Enter paycode (-1 to end): ";
	cin.sync();
	cin >> ans;	
	while (ans != (-1))
	{
		
		while (cin.fail() || (ans < (-1)) || ans >5)
		{
			if (cin.fail())
				cout << "It can not be a letter. Try again: ";
			else
				cout << "Valid score is from 0 to 4, or -1 to end program.";
			cin.clear();
			cin.ignore(10, '\n');
			cin >> ans;

		}
		switch (ans)
		{
		case 1: 
			cout << "Manager selected.\n" << "Enter weekly salary: ";
			cin >> amount;
			cout << "The manager's pay is $ " << amount;
			break;
		case 2: 
			cout << "Hourly Worker selected.\n" << "Enter the hourly salary: ";
			cin >> salary;
			if (salary >= 40)
			{
				cout << "\nEnter the total hours worked: ";
				cin >> hours;
				cout << "Worker's pay is $ " << salary*(hours*1.5);
			}
			else
			{
				cout << "\nEnter the total hours worked: ";
				cin >> hours;
				cout << "Worker's pay is $ " << salary*hours;
			}
			break;
		case 3: 
			cout << "Commision worker selected.\n" << "Enter gross weekly sales: ";
			cin >> weeklysales;
			cout << "Commision worker's pay is $ " << 250 + (.057*weeklysales);
			break;
		default:
			cout << "Pieceworker selected.\n" << "Enter number of pieces: ";
			cin >> pieces;
			cout << "Enter wage per piece: ";
			cin >> priceperpiece;
			cout << "Pieceworker's pays is $ " << pieces*priceperpiece;
			break;
		}
		cin.sync();
		cin >> ans;
		
	}
	return 0;
	
}
Your if statement says (salary >= 40). I'm assuming you wants hours there not salary, cause I would imagine it is just dropping into the else statement right now.
Thanks. I must have just made one of the dumbest mistakes to ever show up on this website. I do have one other question. I need the answers to every switch case to have two decimal places. I know I need to use setprecision(2), but I am unclear on where to put it.
Its been awhile since I've needed to do anything with set precision but I'm pretty sure it goes before the output. I believe it is on this site as well and will give you the details on how to use that properly.
I was just wondering if there was a way to make just one set precision that applies to the entire program.
Topic archived. No new replies allowed.