Hello again. Well, I've been trying to get this to work, and I finally was able to combine two functions into one, but now I have to create a new function called FillTime() which will calculate the time to fill to fill the pool, though it seems that it doesnt want to use the already calculated value for float gallons. any help is appreciated.

#include <iostream>
using namespace std;


void PoolSize(float Length, float Width, float Depth, float volume, const float cubicfeet=7.48051948);

void FillTime(float volume, float FillRate, float Minutes);

void main ()
{
	float Length;
	float Width;
	float Depth;
	float volume;
	const float cubicfeet=7.48051948;
	float FillRate;
	float Minutes;

	cout << "Insert the length: ";
	cin >> Length;
	cout << "Insert the width: ";
	cin >> Width;
	cout << "Insert the depth: ";
	cin >> Depth;
	
	PoolSize(Length, Width, Depth, volume, cubicfeet);

	cout << "Insert the fill rate of the pool in gallons per minute: ";
	cin >> FillRate;
	cout << "\n";

	FillTime(volume, FillRate, Minutes);

	system("pause");
}
//////////////////////////////////Fill Time///////////////////////////////////////////////

void FillTime(float gallons, float FillRate, float Minutes)
{
	Minutes = gallons/FillRate;
	cout <<"The time it takes to fill the pool with a fill rate of "<<FillRate<<" gallons per minute is: "<< Minutes <<" minutes \n\n";
}
//////////////////////////////Volume & Capacity///////////////////////////////////////////
void PoolSize(float Length, float Width, float Depth, float Volume, const float cubicfeet)
{
	
	float gallons;

	Volume = (Length*Width*Depth);
	cout << "The volume of the pool is: "<< Volume << "\n\n";

	
	gallons = (Volume*cubicfeet);
	cout << "The capacity in gallons for this pool is: " <<gallons <<"\n\n";
}

Hello again. Well, I've been trying to get this to work, and I finally was able to combine two functions into one,

Why would you want to do that? Each function had it's individual purpose - leave it at that. A function should do one thing. In your case, one should calculate the volume, one should calculate the capacity.

That's you biggest problem. Split them apart again, and your task is very simple.

Well, the thing is, that is what the assignment says, but thats not the problem, that works. The problem is that the FillTime function doesnt calculate correctly.

The problem is that the FillTime function doesnt calculate correctly.

Because you're passing it garbage. Rather than just give you the solution, I'll point you toward the root of the problem, which is a misunderstanding of pass by value vs. pass by reference.

Okay, so I put a & before the gallons and the fill rate and the minutes too.. so its now a reference value.. so atleast now i get something else than 0 as the time result. but the calculations is still incorrect.. is the / symbol correctly used? on the FillTime()

should it be like a return gallons; after the body of the function?

Still cant make it work.. D:

Edited 5 Years Ago by Ezzaral: Removed unnecessary cursing. Read the forum rules.

Still cant make it work.. D:

Apparently, you still can't make it work because you are lacking clarity on some things. So let's review a few things that I'm sure you've already covered but haven't learned very well (which is why you have these exercises!) I will give you a refresher on it and then with that fresh in mind you can go back and look at what you are doing, and then you can fix the problem yourself.

Variables have scope, remember? They are only accessible within the brackets { } in which they where declared. For example:

int main()
{
   int x = 0;       // int x can only be accessed within the main function, because it was declared between
                    //       main()'s brackets{ }. Therefore, the function "foo" can't even see x (which is 
                    //       why we have to pass things around). 
   
   {
     int y = 0;     // int y only exists on line 8, and doesn't even exist anymore by the time we get to line 10.
   }
                    // If you try to use y now, you will get an error: "y is an undeclared indentifier".

   return 0;        // I always use a return statement at the end of a function, to return control back from whence 
                    //       it came.
 }

//******* function definitions*******

void foo(int x)     // This int x has NO relation to any other int x in the program - it's a separate int x that 
                    //       just came into existance for use by the foo function. In fact, the function foo can't 
                    //       even see outside it's own brackets, so it wouldn't even know if there was another int x 
                    //       out there!
{ 
   cout x;
   int z = 0;       // Here int z only exists within function foo's brackets{}.Go outside of the brackets and z is 
                    //       history.
   
   return;          // The usual return statement to end the function and hop back.
}

OK, so that is the first thing that you need to keep in mind when you go back to look over your program. And the second thing is how functions work.

We will make a function and name it "foo". Now imagine that the planet earth is the main() function in our program and the moon is our function foo(). So our function foo() is a little self contained entity "out there" somewhere in global namespace. And usually, for our function to be useful, it needs information from earth. So earth passes some information to our function and after our function calculates on this information it then passes the result back to earth. So then we have information being passed back and forth between main() and foo().

Now main() has something like a port, a computer port, that sends and receives information to and from functions. And functions have a separate receive port (at the start of the function) and a send port (at the end of the function). Now they are not really called ports or anything, I'm just using that as a metaphor. Here is what it looks like:

include<iostream>
using namespace std;

int foo(int, int, double);  // Just a prototype for the compiler.

int main()
   {
     int x = 10;
     int y = 13;
     double z = 3.1416;

     int sum = 0;

     sum = foo(x,y,z);     // Here is the "port" that main() uses to send and receive data to\from foo().
     cout << sum;

     return 0;             // The usual return statement to return to the OS and end the program.
   }
// ******* function definition *******

int foo(int num1, int num2, double num3) // Here is the receiving port in foo() - num1, num2 and num3 receive 
                                        //       whatever was sent from the port in main().
   {
     cout << num1;         // This will print 10.
     cout << num2;         // This will print 13.
     cout << num3;         // This will print 3.1416.

     num1 = (num1 + num2);
     return num1;          // Here is the sending port for foo(), and this time, we include a value to go with our 
                           //       usual return statement. So then the value of num1 will appear in the 
                           //       send\receive port back in main(), and then it will be transferred to "sum" 
                           //       (in line 14 above).
    }

OK, just a few more points to keep in mind:

1). In the function prototype in line 4, this is just to let the compiler know that there is a function called foo that receives two integers and a double and sends back an int. ---> int foo(int, int, double); You do not need to name the int, int and double (i.e. parameters). You can name them if you want, for your own reference, but the compiler doesn't need it or care and will just ignore any naming of parameters.

2). When we pass a variable, we don't really pass a variable. I mean, we don't pass the variable ITSELF, or even it's name, we just pass the value in that variable. It would be like if the main() function called up our foo() function on the telephone and said, "Let's see, ..... x currently has the value of 10, and y is at 13 and it looks like z is reading 3.1416". Then our foo() function, which is writing this down with pencil and paper, replies, "OK, got it! Thanks". See, just the information is passed, not anything of the variable itself or it's name - just a carbon copy of the numbers in those variables. Therefore, we can name our variables in our foo() function anything we want, because we are just receiving information. If back in main() x, y and z are being sent (actually, just a copy of their values) then that doesn't mean we have to name our variables that receive that information x, y and z. So nothing of the variable is actually being passed to the function. Rather, it is like the values of those variables are being phoned over to the our function foo(), so that it knows what their values are, so that it can do the correct calculations. That's all.

3). Now if we want, we CAN actually mess with the variables themselves back in main() from our foo() function if we want - but that would take a reference or a pointer, neither of which we will get into now. But just so you know.

Now, with this knowledge clearly in mind (I hope it's clearly) go look over your program. Look at what you are doing with your functions, how you pass the volume variable and then don't even use it in the function, etc. Keep in mind the scope of the various variables. You did make a statement about your filltime ? function that apparently couldn't use the gallons variable that was already calculated. Hmmmm..........

This article has been dead for over six months. Start a new discussion instead.