Displays 0.00 instead of Correct Tax.

May 16, 2013 at 8:39pm
For some reason this code only displays 0.00 when it should display monthly tax! PLEASE HELP

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
  #include <iostream>
#include <iomanip>
using namespace std;



double computeTax(double monthlyincome)
{
   double yearlyincome;
   double yearlytax;
   double monthlytax;
   {
   yearlyincome = (monthlyincome * 12);

      if (yearlyincome > 0 && yearlyincome <= 15100)
         yearlytax = (yearlyincome * .10);
      else if (yearlyincome > 15100 && yearlyincome <= 61300)
         yearlytax = 1510 + .15 * (yearlyincome - 15100);
      else if (yearlyincome > 61300 && yearlyincome <= 123700)
         yearlytax = 8440 + .25 * (yearlyincome - 61300);
      else if (yearlyincome > 123700 && yearlyincome <= 188450)
         yearlytax = 24040 + .28 * (yearlyincome - 123700);
      else if (yearlyincome > 188450 && yearlyincome <= 336550)
         yearlytax = 42170 + .33 * (yearlyincome - 188450);
      else if (yearlyincome > 336550)
         yearlytax = 91043 + .35 * (yearlyincome - 336550);

   monthlytax = (yearlytax / 12);
   }
   return (monthlytax);
}

int main()
{
   cout.setf(ios::fixed);
   cout.setf(ios::showpoint);
   cout.precision(2);
   double yearlytax;
   double monthlyincome;
   double monthlytax;
   cout << "Income: ";
   cin >> monthlyincome;
   cout << "\n"
        << computeTax(monthlytax);
   return 0;
}

May 16, 2013 at 8:43pm
try this: double computeTax(double& monthlyincome) <---send by reference
May 16, 2013 at 8:46pm
i put in computeTax(double& monthlyincome) and there was no change in the output :(
May 16, 2013 at 8:51pm
I think line 44 should be << computeTax(monthlyincome);
May 16, 2013 at 8:57pm
foudn it: cout << "\n"
<< computeTax(monthlytax); here you send monthlytax which is zero.. and that's why you get zero.. you need to send monthlyincome..
May 16, 2013 at 8:58pm
That was it!! Thank you! Why is it monthlyincome and not monthly tax I thought that was what was being returned?
May 16, 2013 at 9:05pm
when you send something to other function, it's not necessarily what you want to be returned. it's just something you want to work with and the function can return something else depending on your needs. in that case you sent monthlytax but as you can see the value of it before you send it is NULL. because it's NULL than by this comand: yearlyincome = (monthlyincome * 12); your yearlyincome also became NULL, therefore you got zero all the time
Topic archived. No new replies allowed.