0

I am new to C++ and would like to know why my code isn't working. Whenever i run it, i keep getting 'Tax payable = 0' no matter what value i input for my annual income variable 'aninc' Where is my error please? Find attached the question.

Here is my code

#include <iostream>
using namespace std;
int main() {
  int aninc, dep, tax;

  cout << "Enter your annual income: ";
  cin >> aninc;

  cout << "Enter how many dependants you have: ";
  cin >> dep;

  if (dep < 0) {
    cout << "Invalid input! Number of dependants cannot be negative.";
  } else if (dep >= 0) {
    if (dep > 3) {
      dep = 3;
    }
    switch (dep) {
      case 0:
        aninc = aninc - 255000;
        break;
      case 1:
        aninc = aninc - 325000;
        break;
      case 2:
        aninc = aninc - 395000;
        break;
      case 3:
        aninc = aninc - 455000;
        break;
    }
    if (aninc > 70000) {
      tax = ((15 / 100) * 50000) + ((20 / 100) * (70000 - 50000)) +
            ((25 / 100) * (aninc - 70000));
    }
    if (aninc > 50000 && aninc < 70000) {
      tax = ((15 / 100) * 50000) + ((20 / 100) * (aninc - 50000));
    }
    if (aninc <= 50000) {
      tax = ((15 / 100) * aninc);
    }
    cout << "Tax payable: " << tax << endl;
  }
  return 0;
}

Switch statement Question

I have the following C++ question where i have to implement a switch statement.

Bill Lynch
  • 80,138
  • 16
  • 128
  • 173
Nas
  • 1
  • 1
  • 3
    Read about the effects of integer division. – πάντα ῥεῖ Feb 13 '22 at 18:40
  • 1
    How do you know the switch is not working? It looks fine to me. `15 / 100` and similar are wrong as the comment above talks about. My advice is to spend 20 minutes and learn how to use your debugger to step through your code 1 line at a time instead of just executing your code in your IDE. – drescherjm Feb 13 '22 at 18:41
  • 1
    `(15 / 100) == 0` – Igor Tandetnik Feb 13 '22 at 18:42
  • 1
    If you know that `tax` is not correct, then the way a programmer solves this issue is to *break down the statement and see where the problem is*. Instead of doing that one long calculation, you could have done it in pieces to see where the value is incorrect: `int temp = 15 / 100;` would have signaled that something is wrong. Too many times, I see a new programmer throw a calculation into one long, almost never-ending line of code, and then wonder why the value isn't correct, instead of breaking down the calculation to see where the problem starts. – PaulMcKenzie Feb 13 '22 at 18:45
  • you need to be using floating point numbers ie `10.0/100.0` etc – pm100 Feb 13 '22 at 18:50
  • `int` stands for "integer" which means a number with no fractional component. An `int` could represent `1` or `2` but not any value in between like `1.5`. Any decimals that you may expect to result from any math operation are lost. In the case of `15/100` which should be `0.15` the value is actually rounded to `0` as `int` is not capable of storing decimals. – François Andrieux Feb 13 '22 at 18:55

0 Answers0