adding while loops

adding while loops

how do i fix my code?

now i am trying to add a while loop so the program will only exit if you put 0 or a lower number.


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 <math.h>
#include <string>

using namespace std;


int main()
{

	// declare variables

	int salary; // income
	string Marital_status; //Marital Status
	
	
	string taxFee; 
	double taxPercent; 
	double tax;

	// get input from user

	while (salary <= 0; salary++)
{
    cout << "please enter a valid salary ";
    cin >> salary;
}
	cout << "what is your income?"<<endl;
	cin >> salary;
	cout << "are you married or single?"<<endl;
	cin >> Marital_status;
 

	// processing


		if (Marital_status == "s" || Marital_status == "S") //Single
		{
		if ((salary >= 0) && (salary < 25000))
		{
			taxFee = "single first class tax";
			taxPercent = .10;
		}
		else if ((salary >= 25000) && (salary < 75000))
		{	
			taxFee = "single second class tax";
			taxPercent = .20;
		}
		else if ((salary >= 75000) && (salary < 125000))
		{
			taxFee = "single third class tax";
			taxPercent = .30;
		}
		else if (salary <= 125,000)
		{
			taxFee = "single fourth class tax";
			taxPercent = .40;
		}
		}
		if (Marital_status == "m" || Marital_status == "M") //Married
		{
		if ((salary >= 0) && (salary < 25000))
		{
			taxFee = "married first class tax";
			taxPercent = .08;
		}
		else if ((salary >= 25000) && (salary < 75000))
		{
			taxFee = "married second class tax";
			taxPercent = .15;
		}
		else if ((salary >= 75000) && (salary < 125000))
		{
			taxFee = "married third class tax";
			taxPercent = .25;
		}
		else if (salary <= 125,000)
		{
			taxFee = "married fourth class tax";
			taxPercent = .30;
		}
}
	// do processing

	tax = salary * taxPercent;

	// output results

	cout << "based on the salary" << taxFee << " for $" << taxPercent << endl;
	cout << "your total tax is $" << tax << " per month" << endl;


	system("PAUSE");
	return 0;
}
I would use a do while for this.

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
	while (salary <= 0; salary++)  // I don't get what you were doing here. Remove this.
      
        do
    {                            //begin do while

{
    cout << "please enter a valid salary ";
    cin >> salary;
}
	cout << "what is your income?"<<endl;
	cin >> salary;
	cout << "are you married or single?"<<endl;
	cin >> Marital_status;
 

	// processing


		if (Marital_status == "s" || Marital_status == "S") //Single
		{
		if ((salary >= 0) && (salary < 25000))
		{
			taxFee = "single first class tax";
			taxPercent = .10;
		}
		else if ((salary >= 25000) && (salary < 75000))
		{	
			taxFee = "single second class tax";
			taxPercent = .20;
		}
		else if ((salary >= 75000) && (salary < 125000))
		{
			taxFee = "single third class tax";
			taxPercent = .30;
		}
		else if (salary <= 125,000)
		{
			taxFee = "single fourth class tax";
			taxPercent = .40;
		}
		}
		if (Marital_status == "m" || Marital_status == "M") //Married
		{
		if ((salary >= 0) && (salary < 25000))
		{
			taxFee = "married first class tax";
			taxPercent = .08;
		}
		else if ((salary >= 25000) && (salary < 75000))
		{
			taxFee = "married second class tax";
			taxPercent = .15;
		}
		else if ((salary >= 75000) && (salary < 125000))
		{
			taxFee = "married third class tax";
			taxPercent = .25;
		}
		else if (salary <= 125,000)
		{
			taxFee = "married fourth class tax";
			taxPercent = .30;
		}
}
	// do processing

	tax = salary * taxPercent;

	// output results

	cout << "based on the salary" << taxFee << " for $" << taxPercent << endl;
	cout << "your total tax is $" << tax << " per month" << endl;


     }                                 // end do while
     while (salary > 0);  
Topic archived. No new replies allowed.