anon2000
anon2000

Reputation: 41

Why does this function return 0 even when assigned a value? (C)

Below is the code. When I test it, it keeps returning 0 for some reason.

float compute_personal_allowance ( float annualSalary ) 
{
    int pa = 0;
    if (annualSalary <= 100000)
        pa == 11850;
    else if (annualSalary > 100000)
        pa == 11850 - 1 * ((annualSalary - 100000)/2);
    return pa;
}

I test it using:

gcc -lm -std=c99 -o

Upvotes: 2

Views: 76

Answers (2)

Mike
Mike

Reputation: 4288

to be complete: also the first assignment of pa is wrong:

float compute_personal_allowance ( float annualSalary ) 
{
    int pa = 0;
    if (annualSalary <= 100000)
        pa = 11850;
    else if (annualSalary > 100000)
        pa = 11850 - 1 * ((annualSalary - 100000)/2);
    return pa;
}

Upvotes: 2

Blaze
Blaze

Reputation: 16876

Your problem lies here:

pa == 11850 - 1 * ((annualSalary - 100000)/2);

== doesn't do assignment, it does comparison. So this doesn't actually do anything in this case. It evaluates to 1 or 0 and then just discards that result. What you need instead is

pa = 11850 - 1 * ((annualSalary - 100000)/2);

Upvotes: 3

Related Questions