Running into a small problem trying to validate user input

Hello all. I have put together a program that will calculate gross pay for an hourly or a salaried employee. I am having a bit of trouble with my validation loop
 
while (!(payType  =='H') || !(payType == 'h') || !(payType == 'S') || !(payType == 's'))

It never comes out of this loop even is it type the correct input. Just looking for a point in the right direction.

Here's the full code
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
 #include<iostream>
#include<iomanip>

using namespace std;

union PaySource
{
	int hours;
	double salary;
};

int main()
{
	PaySource employee;
	char payType;
	double payRate, bonus, grossPay;

	cout<<fixed<<showpoint<<setprecision(2);
	cout<<"\tThis program calculates either hourly or salary wages.\n";
	cout<<"\t======================================================\n";
	cout<<"\n";

	cout<<"\tEnter H for hourly wages or S for salary wages: ";
	cin>>payType;
	cout<<"\n";

		while (!(payType  =='H') || !(payType == 'h') || !(payType == 'S') || !(payType == 's'))
		{
			cout<<"\tThe wrong input was made.\n";
			cout<<"\tEnter H for hourly wages or S for salary wages: ";
			cin>>payType;
			cout<<"\n";
		}

	if (payType =='H' || payType =='h')
	{
		cout<<"\tWhat is the hourly pay rate for this employee?\n";
		cout<<"\tPay rate per hour:$";
		cin>>payRate;
		cout<<"\n";

		cout<<"\tHow many hours has this employee worked?\n";
		cout<<"\tHours worked:";
		cin>>employee.hours;
		cout<<"\n";

		while(employee.hours > 80 || employee.hours < 0)
		{
			cout<<"\tThe wrong input was made.\n";
			cout<<"\tPlease enter a positive number of hours less than 80: ";
			cout<<"\tHours worked:";
			cin>>employee.hours;
			cout<<"\n";
		}

		grossPay = employee.hours * payRate;
		cout<<"The gross pay for this employee is: $"<<grossPay<<endl;
	}

	else if (payType =='S' || payType =='s')
	{
		cout<<"\tWhat is the salary rate for this employee?\n";
		cout<<"\tSalary pay rate:$";
		cin>>employee.salary;
		cout<<"\n";
		while (employee.salary<0)
		{
			cout<<"\tThe wrong input was made.\n";
			cout<<"\tPlease enter a positive number for the salary rate: ";
			cout<<"\tSalary pay rate:$";
			cin>>employee.salary;
			cout<<"\n";
		}

		cout<<"\tIs there any bonus this pay period for this employee?\n";
		cout<<"\tBonus rate:$";
		cin>>bonus;
		cout<<"\n";
		while (bonus<0)
		{
			cout<<"\tThe wrong input was made.\n";
			cout<<"\tPlease enter 0 or a positive number for the bonus this pay period: ";
			cout<<"\tBonus rate:$";
			cin>>bonus;
			cout<<"\n";
		}
		grossPay = employee.salary + bonus;
		cout<<"\tThe gross pay for this employee is: $"<<grossPay<<endl;
	}
	system("pause");
	return 0;
} 
Last edited on
while (!(payType == 'H') && !(payType == 'h') && !(payType == 'S') && !(payType == 's'))
Or perhaps
while ( ! (payType == 'H' || payType == 'h' || payType == 'S' || payType == 's') )
Ok
 
while (!(payType == 'H') && !(payType == 'h') && !(payType == 'S') && !(payType == 's'))

that fixed that but, I am getting a strange error. When I input easy numbers like $10 and hour and 10 hours it works fine and tells me gross pay is $100 but when I do $8.75 payRate and 19.5 hours the math goes wrong. It should say gross pay $170.63 but instead it says gross pay $166.25 Anyone got a clue why?
Last edited on
employee.hours; is an integer which means it can't correctly hold the value 19.5
Topic archived. No new replies allowed.