-
C++ functions
I'm trying to calculate the volume, cost, charge, and profit by defining a function and having a function call in the main. I have defined my functions but I guess I don't know how to put the function call in the main, my answers are coming out weird. Can anyone help my with this?
Code:
#include <iostream>using namespace std;
double cal_vol (double l, double w, double h) //volume function
{
double V;
V = l * w * h;
return V;
}
double cal_cost (double V)//cost function
{
double C;
C = V * 0.23;
return C;
}
double cal_charge (double V)// charge function
{
double D;
D = V * 0.5;
return D;
}
double cal_profit (double D, double C)//profit function
{
double P;
P = D - C;
return P;
}
main()
{
int l, w, h;
double cal_vol, cal_cost, cal_charge, cal_profit;
cout << "Enter crate length : ";
cin >> l;
cout << "Enter crate width : ";
cin >> w;
cout << "Enter crate height : ";
cin >> h;
cout << "The crate's volume is " << cal_vol << endl;
cout << "The crate's cost is " << cal_cost << endl;
cout << "The customer's charge is " << cal_charge << endl;
cout << "The profit is " << cal_profit << endl;
system("pause");
}
-
Well I can see a few things that I can see you probably need to add. You never actually call any of the functions you declared you just created variables with the same names of the functions. If you just put something like "volume = cal_vol();" with the needed arguments in the parenthesis and do that for all the other functions I believe it should work as you wanted. I am fairly new to C++ myself so I do not know if this is all you need to do or if there is something more you need to do but hopefully I was able to help you some.
-
thadis_4 has the right idea. The compiler cannot read your mind. As far as it's concerned you've told it about some functions, and some variables (which happen to share the same name, by your choice). Giving a function the same identifier as a variable will not just give these variables the correct values.
For this reason, it's not wise to name your variables the exact same things as your function, even if they are related conceptually. Like thadis_4 suggested, switch the variable names to describe the quantities. cal_vol should become "volume" or "vol", calc_cost should become "cost", etc.
You need to actually fill these variables with the appropriate values by calling the functions in the right-hand side of an assignment.
-
A function call looks like:
Code:
functionName(parameter1, parameter2);
Variable declarations look like:
Code:
double variable1, variable2;
You can't call a variable, nor can you set the value of a function.
-
thanks guys this really helped, i'm new to c++ and programming actually so this place is really helping me out
-
I am just learning too and these forums have helped me a lot too not only by answering questions about programming but just by reading threads that others have posted and looking at their code to try to notice were they might of made a mistake or if I do not know what parts of the program does yet, like classes, I try to guess what the part does.