screw you
screw you

Reputation: 141

why is my if else not working?

I got a nice little function with a 2D array. I want to search for the income in the first array and then use that same int to find the right number in the 2D array.

I have limited my problem down to one line of code. The if Else statement is not working inside the for loop. I don't know why. Any ideas that isn't hitting it with a hammer.

   float get_total_income( float income)
{
    char status[10];
    float over_income = 30001;
    float final_rate = .35;
    float adjusted_income = 0;
    int check = -1;
    int x = 0;
    float rate;
    float array_income[] = { 6000.00, 9000.00, 15000.00, 21000.00, 25000.00, 30000.00 };

    float rates[][6] = { { .028, .075, .096, .135, .155, .174 }, { 0.0, .052, .083, .122, .146, .163 }, 
    { .023, .072, .089, .131, .152, .172 }, { 0.0, .038, .074, .110, .138, .154 } };


    printf("\n\t YOUR INCOME AFTER DEPENDANTS: %.2f", income);


    while (check < 0)
    {
        printf("\n\n\nEnter your Status (S, MJ, MS, SH)");
        gets_s(status);

        if (status[0] == 'S' && status[1] == '\0')
        {
            single_total = single_total + 1;
            check = 0;          
        }
        else if (status[0] == 'M' && status[1] == 'J')
        {
            mj_total = mj_total + 1;
            check = 1;
        }
        else if (status[0] == 'M' && status[1] == 'S')
        {
            ms_total = ms_total + 1;
            check = 2;          
        }
        else if (status[0] == 'S' && status[1] == 'H')
        {
            sh_total = sh_total + 1;
            check = 3;
        }
        else
        {
            printf("\n\n INCORRECT STATUS. Enter (S, MJ, MS, or SH) \n\n");
        }
    }


    if (income > over_income)
    {
        rate = final_rate;
    }
    else
    {
        for (x = 0; x<6; x++)
        {
            printf("num %i# \n", x);

            if (income <= array_income[x])
            {
                rate = rates[check][x];
                printf("\t\nworks, %i# in loop answer: %.3f\n",x, rate);
            }
        }//end for loop
    }//end if

    printf("\n\t YOUR TAX RATE: %.3f", rate);

    return rate;
}

In this part:

for (x = 0; x<6; x++)
            {
                printf("num %i# \n", x);

                if (income <= array_income[x])
                {
                    rate = rates[check][x];
                    printf("\t\nworks, %i# in loop answer: %.3f\n",x, rate);
                }

Instead of stopping to one time to print it is printing the entire table. For some reason the If statement has no power here and the compiler is ignoring it.

What should happen is IF income = 2000 and <= the income_array it should print whatever that part of the array is. However it just keeps going like the conditions are always true.

Upvotes: 2

Views: 558

Answers (3)

lethal-guitar
lethal-guitar

Reputation: 4519

You are never breaking in your loop, so it will always run to the end. If you want to stop iterating as soon as the condition is true, use the following code:

for (x = 0; x<6; x++)
{
    printf("num %i# \n", x);

    if (income <= array_income[x])
    {
        rate = rates[check][x];
        printf("\t\nworks, %i# in loop answer: %.3f\n",x, rate);
        break;
    }
}

Upvotes: 3

arunb2w
arunb2w

Reputation: 1196

Since the income values is passed from another function I cannot guess, what is should be

My suggestion would be printf both the values before if statement **income** and **array_income[x]** and find out the root cause.

And also, You expect if (income <= array_income[x]) should run only one time, but by looking into the array-income values there are possibilities your income value should always be less.

Upvotes: 1

Filipe Gon&#231;alves
Filipe Gon&#231;alves

Reputation: 21213

The loop condition should be while (check == 0), otherwise the loop is never entered because you initialized check to 0

Upvotes: 3

Related Questions