Incorrect answers

I've made a program for an assignment that isn't given me the right answer. The answer should be $4000. Anyone tell me why I get 5000 instead of 4000?

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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// Lab01005.cpp – Calculate child tax credit with 4 functions using an income stipulation
// Created by Taft Sanders on 10/19/2012


#include <iostream>

using namespace std;

int getNumChildren();
double calCredit(int);
int getIncome ();

int main( )
{
   int numChildren = 0;
   double totCredit = 0.0;
   int worth=0;


   numChildren = getNumChildren();

   worth = getIncome();

   totCredit = calCredit(numChildren);



   system("pause");
   return 0;
}

int getNumChildren()
{
   int numChi = 0;
   cout <<"How many children do you have? ";
   cin >> numChi;
   return numChi;
}

double calCredit (int numChi)
{
   double credit = 0;
   int taxCredit=0;
   int worth=0;

  if (worth <= 70000)
   {
       taxCredit= numChi * 1000;
       cout<< "Total tax credit: "<<taxCredit<<endl;
   }

   else if (worth>70000)
   {
       if (numChi>=4)
       {
           cout<<"Total tax credit: 4000"<<endl;
       }
       else if (numChi<4)
       {
           taxCredit=numChi*1000;
           cout<< "Total tax credit: "<<taxCredit<<endl;
       }
   }


   return credit;
}

int getIncome ()
{
    int income=0;

    cout<< "What is your income: ";
    cin>>income;
    return income;
}



How many children do you have? 5
What is your income: 80000
Total tax credit: 5000
Press any key to continue . . .
Your calCredit function sets the user's income to zero.
You set worth to zero , so the first if is evaluated.

You need to send the worth value to the function calCredit, as well as the number of children.
Wow can't believe I didn't see that, excellent job ladies/gents! Thanks again for you help.

For anyone that doesn't understand what just happened, I'll repost. Thanks again for your 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// Lab01005.cpp – Calculate child tax credit with 4 functions using an income stipulation
// Created by Taft Sanders on 10/19/2012


#include <iostream>

using namespace std;

int getNumChildren();
double calCredit(int);
int getIncome ();

int main( )
{
   int numChildren = 0;
   double totCredit = 0.0;
   int worth=0;


   numChildren = getNumChildren();

   worth = getIncome();

   totCredit = calCredit(numChildren);



   system("pause");
   return 0;
}

int getNumChildren()
{
   int numChi = 0;
   cout <<"How many children do you have? ";
   cin >> numChi;
   return numChi;
}

double calCredit (int numChi)
{
   double credit = 0;
   int taxCredit=0;
   int worth
   ;

  if (worth <= 70000)
   {
       taxCredit= numChi * 1000;
       cout<< "Total tax credit: "<<taxCredit<<endl;
   }

   else if (worth>70000)
   {
       if (numChi>=4)
       {
           cout<<"Total tax credit: 4000"<<endl;
       }
       else if (numChi<4)
       {
           taxCredit=numChi*1000;
           cout<< "Total tax credit: "<<taxCredit<<endl;
       }
   }


   return credit;
}

int getIncome ()
{
    int income=0;

    cout<< "What is your income: ";
    cin>>income;
    return income;
}



How many children do you have? 5
What is your income: 80000
Total tax credit: 4000
Press any key to continue . . .
I think you are lucky, that you get the answer you do.

Try testing this with other values less than 70,000 - I doubt it will work.

There is a fundamental problem that I mentioned in my previous post.

If you manage to figure this out, I will explain why it gives the answer it does now. Another clue is that it will always give this answer.
Now your function calCredit sets worth to some random value. You've missed the point completely. You need to pass the value of worth to the function. See how you pass the number of children to the function? Like that.

You can pass more than one value to a function.
Last edited on
Topic archived. No new replies allowed.