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?
#include <iostream>using namespace std;
double cal_vol (double l, double w, double h) //volume function
V = l * w * h;
double cal_cost (double V)//cost function
C = V * 0.23;
double cal_charge (double V)// charge function
D = V * 0.5;
double cal_profit (double D, double C)//profit function
P = D - C;
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;
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:
Variable declarations look like:
You can't call a variable, nor can you set the value of a function.
double variable1, variable2;
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.