Repeat a if-else loop.

What I'm wanting to know is how do I make this whole if else repeat until an invalid number is entered. I believe line 20 (the condition) does not function to do anything at the moment how should I change that?

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>
using namespace std;

 int main()
{

 const double PAY=16.78;
 const double FED=.14;
 const double SOC=.06;
 const double STATE=.05;
 const double UNION=10;
 const int INSUR=35;
 int hours, dependents, base_hours=40;
 double net,gross,h_overtime,p_overtime,soc_tax,fed_tax,state_tax,tot_ded;

 cout.setf(ios::fixed);
 cout.setf(ios::showpoint);
 cout.precision(2);

 while (hours>0)
{
 cout<<"How many hours did you work this week? (Enter 0 when finished)\n";
 cin>>hours; 
 cout<< "How many dependents do you have?(Enter 0 when finished)\n";
 cin>>dependents;

 if (hours>40)
{	if (dependents>=3)
{	   h_overtime=hours-40;
	   p_overtime=(PAY*1.5)*h_overtime;
	   gross=(base_hours*PAY)+p_overtime;
	   cout<<"Your gross pay is $"<<gross<<".\n";
	   fed_tax=gross*FED;
	   cout<<"Your federal tax deduction is $"<<fed_tax<<".\n";
	   soc_tax=gross*SOC;
	   cout<<"Your Social Security deduction is $"<<soc_tax<<".\n";
	   state_tax=gross*STATE;
	   cout<<"Your state tax deduction is $"<<state_tax<<".\n";
	   cout<<"You also have a $10 deduction for union dues and a $35 deduction toward insurance because of your three dependents.\n";
	   tot_ded=fed_tax+soc_tax+state_tax+UNION+INSUR;
	   net=gross-tot_ded;
	   cout<<"Your net take-home pay is $"<<net<<" this week.\n";
}
	else
{	   h_overtime=hours-40;
	   p_overtime=(PAY*1.5)*h_overtime;
	   gross=(base_hours*PAY)+p_overtime;
	   cout<<"Your gross pay is $"<<gross<<".\n";
	   fed_tax=gross*FED;
	   cout<<"Your federal tax deduction is $"<<fed_tax<<".\n";
	   soc_tax=gross*SOC;
	   cout<<"Your Social Security deduction is $"<<soc_tax<<".\n";
	   state_tax=gross*STATE;
	   cout<<"Your state tax deduction is $"<<state_tax<<".\n";
	   cout<<"You also have a $10 deduction for union dues.\n";
	   tot_ded=fed_tax+soc_tax+state_tax+UNION;
	   net=gross-tot_ded;
	   cout<<"Your net take-home pay is $"<<net<<" this week.\n";
}
}
 else
{	if (dependents>=3)
{	   gross=(hours*PAY);
	   cout<<"Your gross pay is $"<<gross<<".\n";
	   fed_tax=gross*FED;
	   cout<<"Your federal tax deduction is $"<<fed_tax<<".\n";
	   soc_tax=gross*SOC;
	   cout<<"Your Social Security deduction is $"<<soc_tax<<".\n";
	   state_tax=gross*STATE;
	   cout<<"Your state tax deduction is $"<<state_tax<<".\n";
	   cout<<"You also have a $10 deduction for union dues and a $35 deduction toward insurance because of your three dependents.\n";
	   tot_ded=fed_tax+soc_tax+state_tax+UNION+INSUR;
	   net=gross-tot_ded;
	   cout<<"Your net take-home pay is $"<<net<<" this week.\n";
}
	else
{	   gross=(hours*PAY);
	   cout<<"Your gross pay is $"<<gross<<".\n";
	   fed_tax=gross*FED;
	   cout<<"Your federal tax deduction is $"<<fed_tax<<".\n";
	   soc_tax=gross*SOC;
	   cout<<"Your Social Security deduction is $"<<soc_tax<<".\n";
	   state_tax=gross*STATE;
	   cout<<"Your state tax deduction is $"<<state_tax<<".\n";
	   cout<<"You also have a $10 deduction for union dues.\n";
	   tot_ded=fed_tax+soc_tax+state_tax+UNION;
	   net=gross-tot_ded;
	   cout<<"Your net take-home pay is $"<<net<<" this week.\n";

}
}
}	   	
 return 0;
}
Last edited on
When you declare house, it isn't initialized to a value, so it could be anything. Set it to some initial value so that the loop condition would be true from the start.
Topic archived. No new replies allowed.