QUESTION:Implement a programmer defined function that computes the volume of a box. Your function should accept three double arguments and then compute and return the volume. (If the computation of the volume is not done inside the function and/or the output is done inside the function you will loose 7.5 pts... the output should be done via a cout statement in main() after the Function has been called. Your program should call the function from main() after asking the user to input the three measurements:
Enter the length of the box: 5.0
Enter the width of the box: 10.0
Enter the height of the box: 15.0
The volume of the box is 750.
#include<iostream>
#include<cmath>
#include <iomanip>
usingnamespace std;
void volumefunc(double);
double length,width,height;
int main()
{
cout<< fixed << showpoint << setprecision (1);
cout << "Please enter the length of the box: ";
cin >> length;
cout<< "\nPlease enter the height of the box: ";
cin >> height;
cout << "\nPlease enter the width of the box: ";
cin >> width;
cout << "\nThe volume of the box is " << volumefunc(volume)<<endl ;
return 0;
}
void volumefunc(double volume)
{
volume =main(length) * main(height) *main(width);
// THIS IS WHERE I AM CONFUSED HOW DO I GET THE LENGTH,HEIGHT, AND WIDTH FROM THE MAIN IN ORDER TO COMPUTE THE VOLUME IN THE VOID FUNC?!?!?!
cout<<volume<<".";
}
make a function that takes 3 arguments and return a double volume
One example:
Arguments are inputed in main() when you ask for input.
call the function in main with the inputed values an print out the volume afte it is returned.
1 2 3 4 5 6 7
double volumeFunc(double valu1, double value2, double value3)
{
// do your calculations
return volume;
}
Okay but I need to get the values in the main and take those values and multiply them inside the void. When I used them in the global scope it does not work. so should I move them into main ?
that means that the prototype takes three as well as they are the same.
So they have to match.
As whether they are global or inside the main it does not matter(preferred inside the main in this case)
as long as the functions are alike.
Hint:
when you call a function in main is like this
1 2 3 4 5 6 7 8 9 10 11 12 13
int main()
{
// variables declerations
// get input and store in the variables
// call the function, this is one version.
double volume = volumeFunc(valu1, valu2, valu3);// this return a double value and assign it to volume
// print out the volume to the screen
return 0;
}
void // <--- means that the function does not return anything
volumeFunc(double length,double height,double width){
//...
return volume; //<---
}
double vol = volumeFunc(length,height,width); //but you want a return value
YES I am blindly guessing.. My goal in life is to annoy everyone on this page and ask dumb questions!... REALLY?!?! No I am not blindly guessing I did not just magically pull this out of my butt ..
I'm new to this.. I have only learned void and int main I have to use both if you looked at my original question .. I put the return in there because it was put in one of the replys. Sorry that I am not a genius so I asked for help.
I have to get the calculation from my void func and output it in my main.. That is what I am trying to figure out because I had a horrible injury while wrestling over the weekend and missed that day in class...
Your function should accept three double arguments and then compute and return the volume.
Since the function needs to return something, making it a void function is clearly wrong. Don't make it a void function. You want to return a double? Well then, the function should look like this: