1

I'm new to the C language, rather programming overall. I was wondering why is it that when I declare a variable to be used within an if statement OUTSIDE the structure, that the output I've received is incorrect (for this piece of code anyway).

Here's my code:

#include<stdio.h>
void grossPay();

int main()
{
    grossPay();
}

void grossPay()
{
int rate = 10, hours;
double tax, grosspay, netpay;

printf("Enter work hours this week: ");
scanf("%d", &hours);

grosspay = hours * rate;

if (grosspay <= 300 && grosspay > 0)
{
    tax = 0.10;
    netpay = grosspay - grosspay * tax;
    printf("Pay for %d hours of week with $%d per hour\n", hours, rate);
    printf("Gross pay: $%.2f\n", grosspay);
    printf("Net pay: $%.2f\n", netpay);
}
else if (grosspay > 300 && grosspay <=1000)
{
    tax = 0.15;
    netpay = grosspay - grosspay * tax;
    printf("Pay for %d hours of week with $%d per hour\n", hours, rate);
    printf("Gross pay: $%.2f\n", grosspay);
    printf("Net pay: $%.2f\n", netpay);
}
else if (grosspay > 1000)
{
    tax = 0.25;
    netpay = grosspay - grosspay * tax;
    printf("Pay for %d hours of week with $%d per hour\n", hours, rate);
    printf("Gross pay: $%.2f\n", grosspay);
    printf("Net pay: $%.2f\n", netpay);
}
else 
{
    printf("Invalid input. Please try again.\n\n");
}
}

Edit: The code I placed was my 'fix' for not getting the correct output. I expected that when I declared the netpay variable once outside the entire IF statement that I would receive the correct output, the same output from the code above.

Edit 2: Previous version

#include<stdio.h>
void grossPay();

int main()
{
    grossPay();
}

void grossPay()
{
    int rate = 10, hours;
    double tax, grosspay, netpay;

    printf("Enter work hours this week: ");
    scanf("%d", &hours);

    grosspay = hours * rate;
    netpay = grosspay - grosspay * tax;

    if (grosspay <= 300 && grosspay > 0)
    {
        tax = 0.10;

        printf("Pay for %d hours of week with $%d per hour\n", hours, rate);
        printf("Gross pay: $%.2f\n", grosspay);
        printf("Net pay: $%.2f\n", netpay);
    }
    else if (grosspay > 300 && grosspay <=1000)
    {
        tax = 0.15;
        printf("Pay for %d hours of week with $%d per hour\n", hours, rate);
        printf("Gross pay: $%.2f\n", grosspay);
        printf("Net pay: $%.2f\n", netpay);
    }
    else if (grosspay > 1000)
    {
        tax = 0.25;
        printf("Pay for %d hours of week with $%d per hour\n", hours, rate);
        printf("Gross pay: $%.2f\n", grosspay);
        printf("Net pay: $%.2f\n", netpay);
    }
    else 
    {
        printf("Invalid input. Please try again.\n\n");
    }
}
mhwk1999
  • 7
  • 4
  • 7
    __Which__ output is incorrect? What output did you expect? – tkausl Nov 10 '18 at 11:44
  • Where is the structure? – Sourav Ghosh Nov 10 '18 at 11:45
  • *"OUTSIDE the structure"* Do you mean outside of the function? In that case I can't repropduce: [A](http://coliru.stacked-crooked.com/a/d18d1038870109c8), [B](http://coliru.stacked-crooked.com/a/651b856a86368900). – HolyBlackCat Nov 10 '18 at 11:48
  • I expected an output of say, when grosspay = 300, that netpay would be = 270 at output. Instead it was grosspay = 300 and netpay = 300. The code I inserted outputs correctly, but it seems a tad tedious and I expected that when I declared the netpay variable once outside the If-statement structure that I would receive the correct output. – mhwk1999 Nov 10 '18 at 11:50
  • In that case, please share the exact snippet of code that does not work correctly. – GoodDeeds Nov 10 '18 at 12:00
  • 1
    Edited and added the relevant piece of code. – mhwk1999 Nov 10 '18 at 12:10
  • Please add all relevant information to the question itself. – melpomene Nov 10 '18 at 12:12

2 Answers2

1

The relevant code boils down to:

double tax;
netpay = grosspay - grosspay * tax;
tax = 0.10;
printf("Net pay: $%.2f\n", netpay);

The problem with this is that statements in a program are executed in the order you've written them (at least within a function, barring special control flow statements such as continue or goto).

Thus:

  1. First we define a local variable called tax, which is uninitialized.
  2. Then we set netpay to the result of grosspay - grosspay * tax. This is already wrong because tax has no defined value at this point, so grosspay - grosspay * tax produces undefined results.
  3. Then we set tax. This has no effect on the value of netpay.
  4. Then we print netpay.

Things are happening in the wrong order. You need to set variables before you use them.

It's like you're telling someone:

  1. Read the book that's in your hand.
  2. Take The Lord of the Rings.
  3. Open it.

And you're wondering why they're not reading from The Lord of the Rings.

melpomene
  • 84,125
  • 8
  • 85
  • 148
0

Maybe you are trying to eliminate the duplicate code. Since the difference between the if blocks is the tax rate, you could set the rate and at the end make one calculation.

#include<stdio.h>
void grossPay();

int main()
{
    grossPay();
}

void grossPay()
{
    int rate = 10, hours;
    double tax, grosspay, netpay;

    printf("Enter work hours this week: ");
    scanf("%d", &hours);

    grosspay = hours * rate;

    if (grosspay <= 300 && grosspay > 0)
    {
        tax = 0.10;
    }
    else if (grosspay > 300 && grosspay <=1000)
    {
        tax = 0.15;
    }
    else if (grosspay > 1000)
    {
        tax = 0.25;
    }
    else
    {
        printf("Invalid input. Please try again.\n\n");
        return;
    }
    netpay = grosspay - grosspay * tax;
    printf("Pay for %d hours of week with $%d per hour\n", hours, rate);
    printf("Gross pay: $%.2f\n", grosspay);
    printf("Net pay: $%.2f\n", netpay);
}
user3121023
  • 8,181
  • 5
  • 18
  • 16
  • Thanks! Was wondering if there was any way to clean out my code as well. Is there any reason why the declaration for netpay has to be after the IF statement and cannot be before it? – mhwk1999 Nov 10 '18 at 12:14