Help with coding

The idea of this program is to allow a small company to enter an employee's ID number, hours worked and rate of pay to determine their net pay after having a few taxes taken out. I have fixed it to where if you try to type in anything other than a number for ID, Rate and Hours it will ask you to enter that info in again, same for going through the process again. I am now trying to see if I can break the coding, and it doesn't look like it. My issue is that if I type in a decimal in for ID, it puts the next question couple lines together, so I'd like to see if someone can help me with these things:

1. How can I code it to where it appears as an invalid input and to try again if someone enters a decimal number in for the Employee ID?
2. It looks like the code has a problem with a high hourly rate and/or high number of hours work, i.e., try 50.00 for hourly rate and 170 for hours worked.
3. I need to also fix it to where someone cannot type in over 168.0 hours since that is the max hours in a 7 day week.

Thanks in advanced.

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
#include <iostream>
#include <iomanip>
#include <limits>
using namespace std;

int main()
	
{
		int empID;
		double rate;
		double hours;
		int ans = 0;
		double OThours;
		double OTpay;
		double gross;
		double Tax;
		double Med;
		double FedTax;
		double Net;

	do
	{
		cout << "Please enter your Employee ID Number: ";
		while(!(cin >> empID))
		{
		cin.clear();
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
        cout << "Invalid input. \nPlease enter your Employee ID Number: ";
		}
		cout << "Now please enter your hourly rate of pay: ";
		while(!(cin >> rate))
		{
		cin.clear();
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
        cout << "Invalid input. \nPlease enter your hourly rate of pay: ";
		}
		cout << "And now, enter the number of hours you worked: ";
		while(!(cin >> hours))
		{
		cin.clear();
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
        cout << "Invalid input. \nPlease enter the number of hours you worked: ";
		}
		OThours = (hours - 40.00);
		OTpay = (rate * 1.5);
		if(hours <=40.00)
			gross =  rate * hours;
		else if(hours > 40.00)
			gross = (OThours * OTpay) + ((rate * 40.00));
		cout << setprecision(2) << fixed << "\nHere is your gross pay: \t\t\t\t$" << gross;

		if(gross < 199.99)
		{
			Tax = gross * 0.062;
			Med = gross * 0.0145;
			Net = gross - Tax - Med;
			cout << setprecision(2) << fixed << "\nHere is the amount being withheld for FICA-OASDI Tax: \t$" << Tax;
			cout << setprecision(2) << fixed << "\nHere is the amount withheld for FICA-Medicare: \t$" << Med;
			cout << setprecision(2) << fixed << "\nHere is your net pay after taxes: \t$" << Net;
		}
		else (gross >= 200.00 && gross < 500.00);
		{
			FedTax = gross * 0.15;
			Tax = gross * 0.062;
			Med = gross * 0.0145;
			Net = gross - FedTax - Tax - Med;
			cout << setprecision(2) << fixed << "\nHere is the amount being withheld for Federal Tax: \t$" << FedTax;
			cout << setprecision(2) << fixed << "\nHere is the amount being withheld for FICA-OASDI Tax: \t$" << Tax;
			cout << setprecision(2) << fixed << "\nHere is the amount being withheld for FICA-Medicare: \t$" << Med;
			cout << setprecision(2) << fixed << "\nHere is your net pay after taxes: \t\t\t$" << Net;
		}
		if (gross >= 500.00)
		{
			FedTax = gross * 0.21;
			Tax = gross * 0.062;
			Med = gross * 0.0145;
			Net = gross - FedTax - Tax - Med;
			cout << setprecision(2) << fixed << "\nHere is the amount being withheld for Federal Tax: \t$" << FedTax;
			cout << setprecision(2) << fixed << "\nHere is the amount being withheld for FICA-OASDI Tax: \t$" << Tax;
			cout << setprecision(2) << fixed << "\nHere is the amount being withheld for FICA-Medicare: \t$" << Med;
			cout << setprecision(2) << fixed << "\nHere is your net pay after taxes: \t\t\t$" << Net;
		}

		cout << "\n\nWould you like to repeat the process for another employee?\nType 1 for Yes, Type 2 for No, then press Enter: ";
		while(!(cin >> ans))
		{
		cin.clear();
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
        cout << "\nInvalid input. \nWould you like to repeat the process for another employee?\nType 1 for Yes, Type 2 for No, then press Enter: ";
		}
		cout << "\n\n";
	} while (ans == 1);

	return 0;
}
You should use getline(cin, variable);
This will insure it takes it the whole line of text the user enters. Make sure you include the <string> library.
So you're saying add
#include <string>

And can you tell me where to use getline(cin, variable);
It would require you to not use a while loop, you would also need to change empID from an int to a string. Let me see if there is another way to get what you want. Give me a sec.
This is an assignment for school, so unfortunately I do have to use a DO Loop, which is why I used the loop I'm using. I can accept #include <string> and turn empID from int to string. I guess theoretically, if an employee ID has a decimal in it, it's not up to me right now to decide that it doesn't, so I'm good with that.

So really, now I just need help with #2 and #3 from my original post.
Last edited on
You can use a do loop still. However, your while loop wont work with the method I created. I am unsure off the top of my head how to prevent decimal entry after your numeric input. However, this is an example of how to prevent it in a string input.
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
#include <iostream>
#include <iomanip>
#include <limits>
#include <string>
using namespace std;

int main()

{
	int empID;
	double rate;
	double hours;
	int ans = 0;
	double OThours;
	double OTpay;
	double gross;
	double Tax;
	double Med;
	double FedTax;
	double Net;

getID:
	string id = "";
	cout << "Please enter your employee ID number: ";
	getline(cin, id);
	if (id.back() == '.') 
	{ 
		cout << "Invalid Input" << endl;
		goto getID; 
	}

	return 0;
}
Last edited on
Ok thanks, I'm going to leave the Employee ID part alone, I think it'll be ok for the assignment, especially since my instructions don't mention that the ID can or can't have a decimal in it. Can you help me with #2 and #3 from my original post?
The first problem I'm noticing is that the max numbers I can enter is 12.00/hr and 41 hours. If I try putting anything higher than those two numbers, it wants to break down the taxes twice. I can't see anything in my code that limits me within those numbers.
Topic archived. No new replies allowed.