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?

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576`` ``````// Lab01005.cpp – Calculate child tax credit with 4 functions using an income stipulation // Created by Taft Sanders on 10/19/2012 #include 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: "<70000) { if (numChi>=4) { cout<<"Total tax credit: 4000"<>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.

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677`` ``````// Lab01005.cpp – Calculate child tax credit with 4 functions using an income stipulation // Created by Taft Sanders on 10/19/2012 #include 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: "<70000) { if (numChi>=4) { cout<<"Total tax credit: 4000"<>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.