Question about function

I wrote this program but im actually suppose to write it as a function..But im having a lot of trouble writing it as a function... Can someone please explain how i would do that...Thx

Calculation functions:
o Write a function to calculate the total cubic feet (Length x Width x Depth) and
return the cubic feet.
Inputs: Length, Width, and Depth
Return cubic feet
o Write a function to calculate the number of gallons:
1 cubic foot = 7.48051948 US gallons (Create a constant to hold the conversion factor.) Input: Cubic Feet Return Gallons




#include <iostream>
#include <cmath>
using namespace std;



int main()
{
const double FEET_TO_GALLONS = 7.48051948;
double length;
double width;
double depth;
double cubic;
double total;

cout << "Enter Length: ";
cin >> length;

cout << "Enter Width: ";
cin >> width;

cout << "Enter Depth: ";
cin >> depth;

cubic = length * width * depth;
total = cubic * FEET_TO_GALLONS;

cout << "total is: " << cubic << " cubic feet" << endl;
cout << "number of gallons are: " << total << endl;

system("pause");
return 0;
}
I believe you will find the following links enlightening:
http://cplusplus.com/doc/tutorial/functions/
http://cplusplus.com/doc/tutorial/functions2/

Also make FEET_TO_GALLONS a global variable (i.e. put its declaration before main, not inside it) so that it can be accessed by your functions.
Last edited on
How about
1
2
3
4
5
6
7
8
9
double CalcVolume(double l, double w, double d){
   double cubic = //???
   return cubic;
}

double FeetToGallons(double feet){
   double total = //???
   return total;
}

?
I guess this is like I am the bad cop and hamsterman is the good cop :D I assume you are guilty (i.e. you haven't read anything about your task subject, which is functions) and I direct you to the tutorial, while hamsterman hands you the core for the definitions of your functions and waits for you to ask for more help, thus proving yourself guilty :P
Last edited on
Relax, fellas. Seeing as your posts were separated by two minutes, it's possible that hamsterman didn't see your post.

First of all, you can't be guilty of ignorance. ...okay, yes you can. However, he may have a teacher that is outright boring. ...okay, that's no excuse. But he may have a very poor teacher. ...shutting up now.

Second, hamsterman, c'mon. You pretty much gave him the solution to his problem, something that you should know that we here frown upon. His problem was that he didn't know how to define the functions, not calculate the volume.

Third, I recommend [code] tags.

Fourth, it looks like everything is in order here, otherwise. Nice work.

-Albatross

P.S.
300-- posts and counting.
ERROR: Lvalue required as decrement operand.
Last edited on
Thank you guys...

By the way just because im having problems with functions does not mean i havent read anything on the subject... This is beginners forum and thats why I came here for help... I am on an online class where the professor tends to ignore all emails.... So i come here to try and get help.
Gee... I do know that feeling. [/nosarcasm]

Seriously, though, m4ster r0shi's links are useful and I recommend you take a peek at them. [/stillnosarcasm]

-Albatross
Last edited on
Topic archived. No new replies allowed.