-5

so I'm tryna make a payment calculator which will ask the user to input a salary and it should show them everything(their net pay, their allowance, their salary etc.) and I am trying to make it so that if the number they put in is within a certain range the rest of the code will use a certain number but I am having a problem which is that only my else if statement works correctly. the if statement would execute but also show the else if statement also. Can you guys help?

Here's the part of code:

    ///Allowance
int catagory()
{
    if ((income <= 150000) && (income >= 80000))
    {
        allowance = 8000;
        printf("\nYour Allowance is: %d", allowance);
    }
    else if((income <= 79999) && (income >= 55000));
    {
        allowance = 6500;
        printf("\nYour Allowance is: %d", allowance);
    }
}
NanDemoni
  • 1
  • 1
  • 9
    General tip for mathematical sanity: Could you please rewrite all your range checks in "increasing" form `if (A <= x && x <= B)`? That makes it infinitely easier to understand. – Kerrek SB Mar 18 '17 at 15:22
  • What is the value of `income` when you get to the `if` statement? – jhh Mar 18 '17 at 15:22
  • This code shouldn't even compile, as your function does not `return` a value – UnholySheep Mar 18 '17 at 15:23
  • Income is whatever the user inputs, so if I input 65000 the allowance will show 6500 but if I put 80000 the allowance will print both 8000 and 6500 – NanDemoni Mar 18 '17 at 15:24
  • that isn't the full code its just that one block, I call it back in the main so it runs fine right now. The rest of the code works fine it's just that block – NanDemoni Mar 18 '17 at 15:26
  • You can simply your conditions; if you know that `income >= 80000` is false, you also know that `income <= 79999` is true. – molbdnilo Mar 18 '17 at 15:26
  • @UnholySheep, It's a well-formed C and C++ function, so compiling makes sense, but it shouldn't compile without a warning for sure. On that note, the other mistake should have a warning as well. OP, Please make sure you have warnings enabled. – chris Mar 18 '17 at 15:32
  • @chris It's kind of stupid to have a non-void returning function without a return statement be undefined behavior, but hey... that's how it is... – DeiDei Mar 18 '17 at 15:38
  • I'm still kinda new to programming that's why I asked for help 0.0 thanks for the feedback tho – NanDemoni Mar 18 '17 at 15:45
  • @DeiDei, The compiler can't determine this in all cases. There's some more discussion on that [here](http://stackoverflow.com/questions/1610030/why-does-flowing-off-the-end-of-a-non-void-function-without-returning-a-value-no). Note that being undefined behaviour can help. If you switch over all enum values, the UB will help the compiler to assume nothing outside of the enum can never happen and generate better code. – chris Mar 18 '17 at 16:52

2 Answers2

5
else if((income <= 79999) && (income >= 55000));
                                              ^^^

Take out that semi-colon. It should run correctly now.

DeiDei
  • 10,205
  • 6
  • 55
  • 80
  • 3
    Long live [1TBS](https://en.wikipedia.org/wiki/Indent_style#Variant:_1TBS_.28OTBS.29) :-) – pmg Mar 18 '17 at 15:44
0

Firstly, you have a terminating character at the end of your else-if header:

else if((income <= 79999) && (income >= 55000));

The semicolon splits the else-if header fom its body, makin the body non-conditioned executable part of your function. To fix this, just remove the semicolon:

else if((income <= 79999) && (income >= 55000)) // no semicolon needed

Also, your function is supposed to return an int value, based on te definition of the function catagory(). You need to either add a returnstatement at the end of your function or change the return type of your function to void.

BusyProgrammer
  • 2,783
  • 5
  • 18
  • 31