Question:
C++ Combining functions?
?
2011-11-04 07:59:46 UTC
When I compile the code below the compiler states that “numChildren and totChildren are undeclared. I’ve been jerking with this all morning and I can’t get it to work. Any suggestions? Here are the instructions. Thanks!

Rewrite this program with three functions: a main() function, a function getNumChildren() that asks for the number of children, and a function calCredit( ) that calculates the total amount of child tax credit. The main() function first calls getNumChildren() to get the number of children from the user. It then calls calCredit() to calculate the total credit the user can claim. At the end, the main() function displays the total credit on the screen.

#include

using namespace std;

int main( )
{
numChildren ( );
totChildren ( );
system ("pause");
return 0;
}

int numChildren ( ) //function that calculates number of children

{
int numChildren = 0;
int totChildren = 0;

cout <<"How many children do you have? ";
cin >> numChildren;
return 0;
}

int calCredit( ) //Function that calculates tax credit
{
totChildren = numChildren * 1000;
if (totChildren > 4000)
{
cout << "Total child tax credit: " << totCredit << endl;
totChildren = 4000;
return 0;
}
}
Four answers:
Jeroonk
2011-11-04 08:14:59 UTC
When the compiler encounters the call to "numChildren ( );" in the main, it has not yet seen the declaration of "int numChildren ( ) { ... }" further down below. It doesn't recognize the function, and thus exits with an error.



What you need to do is give the compiler a heads-up about a function called numChildren being declared further down, before you actually call it. This is called a *prototype*, you tell the compiler what the name, return type and expected parameter types of a function are before you call it.



You need to place this prototype at the start of your source file, before the function call. Normally, prototypes are placed in header files (someheader.h), because those are included at the top of your actual source files, before any function calls.



http://pastebin.com/MzVQr0Kq





Now we are not out of the woods yet. There are still errors.



You are trying to call the function "totChildren();" from main, you probably meant to call "calCredit();".



The second problem is that you have named a variable "numChildren" which is the same as a function name "numChildren()". When the compiler sees a reference to "numChildren" it wont know if you meant the variable or function, it thus exists with an error. We can prevent this by renaming any of the two, I renamed the function to getNumChildren().



http://pastebin.com/LqP7fjZ2





The next problems are the "int numChildren = 0;" and "int totChildren = 0;". Those are placed inside the function body of getNumChildren. This means that they can only be used inside that function body, and are removed as soon as the function is done.

When "calCredit()" is called, it tries to access the variables "totChildren" and "numChildren", which don't exist.



What you need to do, is return the value of "numChildren" from the "getNumChildren()" function, and give it to "calCredit" to do a calculation with:



http://pastebin.com/G0bEg6B0 (note how I changed the function prototypes and declarations too)





And now the "calCredit()" function. The logic does not make any sense, and the variable "totCredit" is never declared.

What you probably meant to do was calculate "totCredit", instead of "totChildren". If totCredit > 4000 then you cap it at 4000. And after all that is done, you output the credit.



http://pastebin.com/Tx4vTC50





Last but not least: "system" is a function which is situated in the C standard library, it is not really recommended to use it just for pausing a program, but you can call it by including "#include ".



http://pastebin.com/ig6uqxQx (not recommended, but works)



You are better of using cin.get() though. This makes use of the native I/O library, and does not defer to some external system executable. For more info why system("pause") is bad: http://www.gidnetwork.com/b-61.html.



Final version: http://pastebin.com/m1WFyz7h
green meklar
2011-11-04 08:49:24 UTC
Okay, there's a counterintuitive property of C++ wherein each function must be declared higher in the source code than any calls to it (this is not the case in Java and many other languages, which is why it trips a lot of people up when using C++ for the first time). So if you have no functions that may form part of a recursive loop (or if any such functions are only recursive by immediately calling themselves), which is most likely the case in your program, you can just copy every function definition up somewhere above the first place where it gets called, and then everything works.



If you DO have functions that form part of a recursive loop longer than length 1, this no longer works. In that case, you have to make use of another trick, namely, the fact that you may declare a function before actually defining it. Of course, you can do this even for functions that aren't recursive if you really want to. For instance, I could write something like this:



int whatever(int x);



int main()

{

cout<
return 0;

}



int whatever(int x)

{

return (x+5);

}



Then if I ran the program, it would print out the number 8. See how that works? I declared whatever() before its caller, main(), but didn't define it until afterwards. You should be able to see how this principle could be used in your own code.
Jared
2011-11-04 08:23:42 UTC
You can do this but you should have functions that have separate names from variables inside, otherwise, it becomes a recursive function. You're also somewhat attempting to use globals...usually a poor choice.



#include

int numChildren()

{



int num = 0;

int total = 0;



cout << "How many children do you have? ";

cin >> num;



return num;

}



int calCredit( int num )

{

int credit,totChildren;



totChildren = num*1000;



if (totChildren > 4000){

credit = totChildren;

cout << " Total child tax credit: " >> cred << endl;

return credit;

}

else{

return 0;

}

}



int main()

{

int numKids,credit

numKids = numChildren();

credit = calCredit(numKids);



cin.get()

return 0;

}
?
2016-10-13 08:03:38 UTC
What errors are they? Naming an array 'array' is generally a undesirable thought. additionally there is somewhat some floating numbers here that are actually not defined... why the modulus 89? additionally, on the 2nd 0.5 you are going to need an area after the : in standard. is this meant to all be one application? if so, you are able to not declare 2 arrays the two named array, so call the 2nd something else. additionally, what's int x for? You assign a random fee to it yet under no circumstances use it.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...