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.
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;
}
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){}
elseif(married){}
elseif( filing separate ){}
else {} //dont really need else if head since that is the only other option
@ 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.
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.
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.