### 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

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980`` `````` // // Program: tax.cpp Date: September 27th, 2013 // Programmer: Anton Siedlecki // // #include 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.

 ``123456789`` ``````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!
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293`` ``````// // Program: tax.cpp Date: September 27th, 2013 // Programmer: Anton Siedlecki // // #include 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.