How do i use a Multiway If Else statement in a Loop?

Hey, I'm having trouble using a loop statement in a multiway if else statement. Is there anyway i can run my code using a loop statement to give the user the amount in tax and keep asking them until they enter a 0 or 0.0

I really appreciate any help i can get! Thank you

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
  //
// Program:    tax.cpp		Date:  September 27th, 2013
// Programmer: Anton Siedlecki
//
//
#include <iostream>
using namespace std;


int main() 
{ 
	bool runAgain = true; 
    while (runAgain) 
    { 
        runAgain = runOnce(); // run once and note whether to go again 
    } 

    return 0; 
} 

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

	int incomeYearly;
	double incomeTax,taxRate;
		
		cout << "Enter yearly income amount (0.0 to quit):$ ";
		cin  >> incomeYearly;
		
		if (incomeYearly <= 50000.00)
		{
			taxRate=0.01;
			return true;
		}

		else if (incomeYearly >= 50000.01 && incomeYearly <= 75000.00)
		{
			taxRate=0.02;
			return true;
		}

		else if (incomeYearly >= 75000.01 && incomeYearly <= 100000.00)
		{
			taxRate=0.03;
			return true;
		}
		
		else if (incomeYearly >= 100000.01 && incomeYearly <= 250000.00)
		{
			taxRate=0.04;
			return true;
		}

		else if (incomeYearly >= 250000.01 && incomeYearly <= 500000.00)
		{
			taxRate=0.05;
			return true;
		}

		else if (incomeYearly >= 500000.01)
		{
			taxRate=0.06;
			return true;
		}

		else if (incomeYearly == 0)
		{
			cout << "Processing Completed..";
			return false;
		}
		
		incomeTax=incomeYearly*taxRate;

		cout << "The U.S. 1913 income tax = $" << incomeTax << endl;
		cout << endl;
	return 0;	
}
You should check for incomeYearly==0 first, not as the last else if.
Okay, so first of all, you might want to change int incomeYearly to double incomeYearly. Technically, you don't need to, but whenevery I'm working with decimals, I try not to use int because it chops off the decimals.

I noticed you put a return true; in every if-statement. That tells the program to end the function right there. Literally. It "returns". So you don't want return statements in there.

There are many ways to do what you want, but one way I can think of right now is to create a variable that equals 1 or zero. So instead of putting the return false; statements, you write counter = 1; //assuming you created a variable named counter..and dont include this comment, lol .

And then at the bottom, you can put the cout statement inside an if-statement.

1
2
3
4
5
6
7
8
9
if (counter == 1){
    //cout statement here.
     return true; //Here is where you return. The function is done. 

}

else{//none of the if-statements were executed, meaning the user must have entered in zero, so counter stayed at 0.
     return false;
}


Last edited on
Thank you guys for your advice i changed this up and did the following!
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
//
// Program:    tax.cpp		Date:  September 27th, 2013
// Programmer: Anton Siedlecki
//
//
#include <iostream>

bool runOnce(void);

using namespace std;

double incomeYearly;
double incomeTax;

int main() 
{ 
	bool runAgain = true; 
    while (runAgain) 
    { 
        runAgain = runOnce(); // run once and note whether to go again 
    } 
	
    return 0; 
} 

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


		cout << "Enter yearly income amount (0.0 to quit):$ ";
		cin  >> incomeYearly;

		{
			if (incomeYearly == 0)
			{
				cout << "Processing completed...";
				return false;  
			}
		
			if (incomeYearly <= 50000.00)
			{
				incomeTax=incomeYearly*0.01;
				cout << "The U.S. 1913 income tax = $" << incomeTax << endl;
				cout << endl;
				return true;
			}

			else if (incomeYearly <= 75000.00)
			{
				incomeTax=incomeYearly*0.02;
				cout << "The U.S. 1913 income tax = $" << incomeTax << endl;
				cout << endl;
				return true;
			}

			else if (incomeYearly <= 100000.00)
			{
				incomeTax=incomeYearly*0.03;
				cout << "The U.S. 1913 income tax = $" << incomeTax << endl;
				cout << endl;
				return true;
			}
			
			else if (incomeYearly <= 250000.00)
			{
				incomeTax=incomeYearly*0.04;
				cout << "The U.S. 1913 income tax = $" << incomeTax << endl;
				cout << endl;
				return true;
			}

			else if (incomeYearly <= 500000.00)
			{
				incomeTax=incomeYearly*0.05;
				cout << "The U.S. 1913 income tax = $" << incomeTax << endl;
				cout << endl;
				return true;
			}
	
			else if (incomeYearly > 500000.00)
			{
				incomeTax=incomeYearly*0.06;
				cout << "The U.S. 1913 income tax = $" << incomeTax << endl;
				cout << endl;
				return true;
			}
		}

	return 0;	
}
Topic archived. No new replies allowed.