SOOO CONFUSED can someone help me figure out how to get the l,h,w from main into my void func?

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.



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include<iostream>
#include<cmath>
#include <iomanip>
using namespace 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<<".";
}



pleasee help me!!!
length, width and height are not in main. They are in global scope and can be used in all functions the same way.
It says on your requirements:

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 ?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include<iostream>
#include<cmath>
#include <iomanip>
using namespace std;

void volumefunc(double);


int main()
{
double length,width,height;

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<<endl ;

return 0;
}

double volumeFunc(double length, double width, double height)
{
double volume;
	volume =length* height *width;
	cout<<volume<<".";
return volume;
}




And if I do keep them global then Why is it giving me errors when I run it like this


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include<iostream>
#include<cmath>
#include <iomanip>
using namespace std;

double length,width,height;
void volumeFunc(double);

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<<endl ;

return 0;
}

void volumeFunc(double volume)
{
     volume =length* height *width;
     cout<<volume<<".";

return volume;
}
Look at my above comment closer

the function takes thre arguments,

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;
}


this should help you
Last edited on
Okay so What am I doing wrong in this one then? because my errors say I cannot make a void into a double.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
void volumeFunc(double,double,double);
int main()
{
double length,width,height;  //declaration

cout<< fixed << showpoint << setprecision (1);
cout << "Enter the length of the box: "; //input and stored
     cin >> length;
cout<< "\nEnter the height of the box: ";
     cin >> height;
cout << "\nEnter the width of the box: ";
     cin >> width;

double vol = volumeFunc(length,height,width); //call

cout << "\nThe volume of the box is " <<vol<<". "<<endl ; // print out

return 0;
}

void volumeFunc(double length,double height,double width)
{
	double volume;
volume=length* height *width; //calculations
	
return volume;
}



Thank you for helping me btw !!
Read your code.
1
2
3
4
5
6
7
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 


¿Are you just blindy guessing?
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:

double volumeFunc(double length,double height,double width)
Thanks!! It worked
Topic archived. No new replies allowed.