Yes the calculation of tax. The single "s" calculation works flawlessly however when "m" is input the tax value is not determined. i am debating if and else statements.
You're using switches incorrectly. Let me add some comments so you understand:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
// If in_marital_status is equal to "s"
if (in_marital_status == "s")
{
// case_no is assigned either true/false (or 1/0) depending on
// the result of the following expression
case_no=(income <= single_level1) || (income<= single_level2);
// You're placing case_no into a switch
switch (case_no)
{
// Same as writing
// if (case_no == 1)
case 1: tax = rate1 * income; break;
// else if (case_no == 2)
case 2: tax = single_tax1 + rate2 * (income- single_level1);break;
// else if (case_no == 3)
case 3: tax = single_tax2 + rate3 * (income-single_level2);break;
}
}
The biggest issue seems to be I have no clue what you're trying to assign case_no as, and the computer is taking a guess. You never want the computer to guess at what your code means (actually, it's doing exactly what you told it to do, but it's not what you want it to do).
What is supposed to happen during your switch statements? What is case_no supposed to represent? It might be easier for you to write it out as if statements first and then try to convert it to a switch. Also, switches are designed for constant values only, IIRC, so you can't quite use it as efficiently as an if condition.
further more looking at it. I did do it wrong. However I am no stuck on what the variable is. for the switch i understand a expression is needed. income is incorrect.
You're missing the concept of switches. You're trying to use it like an if statement, and, to an extent, they are. Using the above code, you're doing this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
if (in_marital_status == "s")
{
switch (income)
{
// if (income == 1)
case 1:
// This statement does nothing
(income <= single_level1);
tax = rate1 * income; break;
case 2: (income <= single_level2);
tax = single_tax1 + rate2 * (income- single_level1);break;
default: tax = single_tax2 + rate3 * (income-single_level2);break;
}
}
You're trying to check if the income is less than equal to single_level1, but switches don't work like that. In a case like that, you would want to use an if condition. Rewriting your above code to use switches, you want something like:
Because line 17 isn't doing what you expect it to do. Let me show you what I see (and similarly, what the compiler recognizes it as):
1 2 3 4 5 6 7 8 9
else
// This line won't do anything
(in_marital_status== "m"); //Married Marital Status
// Everything down will be run every time
{
if (income <= married_level1)
tax_bracket=1;
// ...
}
I believe, instead, what you wanted was to change the else to an else if:
1 2 3 4 5
elseif (in_marital_status== "m"); //Married Marital Status
{
if (income <= married_level1)
tax_bracket=1;
}
@rynzor6
You need to look into a code beautifier. It's hard to read the indentations. Otherwise, I'd suggest switching all your spaces to tabs or tabs to spaces. MSVC++ (I believe that's what you're using) should have a replace feature that should make going from what you have to one of the aforementioned indentation styles very quick.
Otherwise, I hope that solves your issues.
Edit: Also line 117's if statement only applies to line 118. You will see "Goodbye!" every time, even if you don't type 99 in for the last name.
To make it not do this, you need to use brackets to signify a code block:
1 2 3
if (last_name == "99") {
more_records=false;
cout<< "Goodbye!";}
Edit2: The semicolon at line 93 in your above code, the else if that I told you to fix. I missed the semi colon at the end, that was my fault.