Code Help Needed Variable initialization error

Here is my Code the goal is to calculate the pool size(volume), then convert to gallons, then to calculate the cost to fill the pool using a fill rate of 10 minutes (will be user input).

I am getting the Run time error #3 for not initializing the variables: Volume and Minutes.

Update #1 It also is cutting me off after calculating time it takes to fill the pool aka not letting me run the cost part of the function, And I'm not catching why I don't have a return code before that section of the code.

[‎code] #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";
}
///////////////////////////////////////COST//////////////////////////////////////////

void Cost (float gallons, float FillRate, float const FlatRate=15.50)
{
float Cost;
Cost = (gallons*FillRate+FlatRate);

cout << "The total cost in U.S. Dollars is: " <<Cost<< "\n\n";
} [/code‎‎‎]


Thanks in advance.
Last edited on
I am getting the Run time error #3 for not initializing the variables: Volume and Minutes.


PoolSize(Length, Width, Depth,volume, cubicfeet); FillTime(volume, FillRate, Minutes);
You're sending uninitialized values for volume and Minutes to the functions. FillTime needs to know the volume, but since that's assigned locally in PoolSize, the main function doesn't know the volume that was calculated. Do these need to be void functions? You could pass the variables by reference, or have the functions return a value instead of being void.

Update #1 It also is cutting me off after calculating time it takes to fill the pool aka not letting me run the cost part of the function, And I'm not catching why I don't have a return code before that section of the code.


I don't see where you're calling the Cost function from Main. I'm not sure exactly what you mean by a return code? The functions are void, so they don't return any value to Main.
How would I have the functions return a value instead of being void?
Here's an example. (I generally try to use different variable names in functions to avoid confusion.)

1
2
//function prototype - just tells compiler return type, function name, number and type of parameters
float PoolSize(float, float, float, const float);


1
2
//function call - store value returned by PoolSize in volume variable, sends copies of the four variables by value
volume = PoolSize(Length, Width, Depth, cubicfeet);


1
2
3
4
5
6
7
//function definition, includes names of parameters used locally, returns a value to the calling function
float PoolSize(float L, float W, float D, const float cf)
{
float value=0; //local variable
//do stuff to calculate value
return value;
}
Topic archived. No new replies allowed.