Home > Blockchain >  Calculation within if statement under switch
Calculation within if statement under switch

Time:10-04

I am trying to write code that allows a user to input a filing status and the income to calculate the tax owed. if filing status input is outside of 1-4 , program will exit. I'm trying to use the switch and if else if, but looks like I am stuck with the code generating the tax owed portion. whenever I input the income, it will not generate the tax owed amount. example, for case 1: in switch (last line) for filing status I input 1(for single) and input 20000 for income. the result should show $3000 but it will not give any result. below is the code I have so far.

#include<stdio.h>

int main()
{

//declaration  status = filing status , income = income input, taxOwed = tax owed calculation.
int status;
double income, taxOwed;

//data/input
printf("************Menu****************\n");
printf("1) Single\n");
printf("2) Married Filing Jointly\n");
printf("3) Married Filing Separately\n");
printf("4) Head of Household\n");
printf("5) Exit\n");
printf("\n");
printf("********************************\n");
printf("\n");
printf("Enter status : ");
scanf("%d", &status);

//processing switch with Calculation within each case
switch(status)
{
    case 5:
        printf("\n", status);
        printf("Exit Program...\n");
        break;
    case 6: case 7: case 8: case 9:
        printf("You entered a wrong status. Program Exit . . .");
        break;
    default:
        printf("You entered a wrong status. Program Exit . . .");
        break;
    case 1:
        printf("Enter your taxable TI: $");
        scanf("%.2lf\n", income);
        if (0 < income <= 24000)
        {
            taxOwed = (income * 0.15);
            printf("\n");
            printf("The taxes owed are: $%.2lf", taxOwed);
            break;
        }
        else if (24000 < income <= 58000)
        {
            taxOwed = 3600   (0.28 * (income - 24000));
            printf("The taxes owed are: $%.2lf", taxOwed);
            break;
        }

}

return 0;
}

any review and help is much appreciated. thank you

CodePudding user response:

NOTE: This answer applies to revision 2 of the question. Meanwhile, OP has applied several of my recommended fixes to the code in the question, thereby invalidating my answer.

The statement

scanf("%.2lf\n", income);

is wrong, for three reasons:

  1. The function scanf expects the address of the variable it should write to, not the previous value of the variable.

  2. It is unclear what the you intend the .2 to do. You are probably confusing the scanf format string with the printf format string. This is probably causing scanf to fail. You should always check the return value of scanf, to see if the function was successful or not.

  3. The \n in the format string will cause scanf to continue reading whitespace, until it finds a non-whitespace character. This means that you must press the ENTER key at least twice and must provide additional input. This is probably not what you want.

Therefore, you should change that line to the following:

scanf("%lf", &income);

Also, the statements

if (0 < income <= 24000)

and

else if (24000 < income <= 58000)

are wrong.

They should probably be

if (0 < income && income <= 24000)

and:

else if ( 24000 < income && income <= 58000 )

The reason they are wrong is the following:

According to the rules on operator precedence, the expression

24000 < income <= 58000

is equivalent to:

(24000 < income) <= 58000

This means that this expression is equivalent to either

0 <= 58000

or

1 <= 58000

depending on whether (24000 < income) evaluates to true (1) or false (0).

In both cases, the entire expression will evaluate to true, because 58000 is larger than both 0 and 1.

That is why writing

if (0 < income <= 24000)

and

else if (24000 < income <= 58000)

does not make sense, because these statements are equivalent to writing

if ( 1 )

and:

else if ( 1 )

  • Related