Tax

closed account (G1pE3TCk)
Hey, all! I'm having a minor issue with my code so far for this program I am working on. It is a program to calculate the income tax based on marital status and income. I'm not sure if I have my variable labeled correctly. I'm new to C++ so any help would be greatly appreciated.
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
   
    char status;
  

    cout <<"Enter filing status: S for single, M for married, F for filing 
    separately, and H for head of household.";
    


    cout <<"Status: ";
    cin >> status;
    cout <<endl;
    
    if (status=='S')
    {
        cout <<"Please enter your taxable income: ";
        cin >> taxableincome;
        cout <<"s-test";
    };
    if (status=='M')
    {
        cout <<"Please enter your taxable income: ";
        cin >> taxableincome;    
        cout <<"M-test";
    };
    if (status=='F')
    {
        cout <<"Please enter your taxable income: ";
        cin >> taxableincome;
        cout <<"F-test";
    };
    if (status=='H')
    {
        cout <<"Please enter your taxable income: ";
        cin >> taxableincome;
        cout <<"H-test";
    }
    return 0;
}
Last edited on
Consider your logic. According to the code, you can be single and married...
Is that what you want?
closed account (G1pE3TCk)
not quite sure what you mean?
My bad. Never mind the above. What do you think is going wrong with your code?

You might need more variables. ie.e you can be married and be the head of household. How do you capture this?
Last edited on
closed account (G1pE3TCk)
I forgot to include the definitions for the varables S,M,F,H. But I think there is also an issue with the "cin >> status."
Hiya.
I'm not sure if I have my variable labeled correctly.


There are a lot more worrying things in there. For example:

cin >> status<<endl;

That's pretty mental :)

Also you want to test for the character in your if's. Your compiler is treating your S,M,F and H as undeclared variables.
closed account (G1pE3TCk)
I'm not sure what you mean by "test for the character in your if's.."
get rid of thoese variables you've added for starters.

add one for taxableincome.

And i mean you need something like this:

if (status=='S')

i.e. you are telling your compiler you want to test for the literal character 'S'.
Last edited on
closed account (G1pE3TCk)
I made the final change you suggested. it compiles, but it doesn't cout the "S-test" stated in the if statement.
works on my box.

did you type 's' and not 'S'?
Last edited on
closed account (G1pE3TCk)
good call! Thank you!
No one else mentioned this but you should never put a bunch of if statements like that when it is clearly a if its this do that..otherwise do something else.
You should use if/else if

1
2
3
4
if(single){}
else if(married){}
else if( filing separate ){}
else {} //dont really need else if head since that is the only other option 


Same with this situation

1
2
3
4
5
if( grade < 60 ) std::cout << 'F' << std::endl;
else if( grade < 70 )  std::cout << 'D' << std::endl;
else if( grade < 80 ) std::cout << 'C' << std::endl;
else if( grade < 90 ) std::cout << 'B' << std::endl;
else std::cout << 'A' << std::endl;


is equal to this

1
2
3
4
if( grade < 60 ) std::cout << 'F' << std::endl;
if( grade > 59 && grade < 70 ) std::cout << 'D' << std::endl;
if( grade > 69 && grade < 80 ) std::cout << 'C' << std::endl;
//repeat.... 
Last edited on
@ giblit.
Yes, that is what threw me of in my replies above. I think you express well the situation. That is why I was asking can you be single and married at the same time. Obviously not. Therefore the if conditions are related to each other. Making the use of an else statement wiser. By using the else, as soon as one if condition is met. the others within that block will be skipped.
One step at the time in my opinion.

fixing his syntax was my first priority. logic comes (or would have come) next.
Yeah but he edited his original code before I could even see it.
yea :)
closed account (G1pE3TCk)
I know practically nothing about code nor programming, but why does it matter if I have just a bunch of if statements instead of if/else statements?

in my experience (which is about 3 weeks), the "else" part always messes up my program. I'm probably typing it wrong, seeing as how I am not a CS major nor trying to become a programmer etc. I am only taking this course to fulfill a requirement to graduate in another 3 months. But for the time being, is how I am using the if statements "technically" wrong?
basically this If you use a bunch of if statements like in your program and the person is single it will check the first and it will be true then do the stuff inside then it will check all of the other if statements and see if any of those are true which could cost you a lot. If you do what I did with if/elses then it will check until it finds a true condition then it will skip over the rest so say for example they were single then it would do the first if statement then skip over all the else's. So your programs will run faster.
It is also better because you have to type less because the else if will check the previous condition before checking the next so you don't have to retype that just like in my grade example http://www.cplusplus.com/forum/beginner/113127/#msg618190

*edit didn't see your question at the end

technically it is not wrong but it is highly recommended to use if/else if they are related statements. If they are not related then yeah it is perfectly fine to use a bunch of if statements.
Last edited on
Imagine if you had 10,000 doors in front of you. The first door has a window in it and you can see behind it something that makes you realise that that is the door you need to choose. There's no point checking the rest of the 9,999 doors. That's why if/else is used.
That's not how switches are used. That is how if if if if if if is used. Switches are like this...int is 123 jumps directly down to 123 doesn't even check 1-122 or after 123. If/elses are like this..check all the if/esles in order until it finds the if( 123 ) clause.
Topic archived. No new replies allowed.