Problem with my program using functions

Hi,im building a Flight calculator program, which calaculates the fuel consumption needed for a plane. Im using functions and the program runs fine, but when I input data it comes with a strange number like this 3.067+e45. I cant figure out whats wrong.

Heres my code:

#include <cstdlib>
#include <iostream>

using namespace std;

void fuel(float & dist);
void distance(float dist, float & time, float & time2 );
void Altitude(float time, float time2, float altlevel);
void Climb_Descend(float altlevel);

int main(int argc, char *argv[])
{
float altlevel;
float dist;
int fuelcon;
float speed=300, altspeed;
char choice;
bool OK;




do
{

fuel(dist); //calls
distance(dist, speed, altspeed);
Altitude(speed, altspeed, altlevel);
Climb_Descend (altlevel);



cout <<"do you want to calaculate again?"<<endl;
cout<<"enter Y or N"<<endl;
cin>>choice;

if(choice=='Y' || choice=='y')
{
OK=true;
}
else
OK=false;

}
while (OK==true);



system("PAUSE");
return EXIT_SUCCESS;
}

void fuel (float & dist) //function
{
cout<<"Enter the distance of flight "<<endl;
cin>>dist;



}

void distance(float dist, float & time, float & time2)
{
time = dist/300; // Speed is constant 300mph
time2 = (time - 0.30)*60; //30 minutes is needed to get to the top level

cout<< " the flight will last "<<time<<" hours "<<endl; // output
}

void Altitude(float time, float time2, float altlevel) //function
{
float Level1,Level2,Level3,Level4,Level5,Level6,Level7,Level8,Level9; // each level goes from 0 to 40,000 feet,

float climb, descend, topFly;
int min;

min = 10;

Level1 = 12,5 * min;
Level2 = 11,33 * min;
Level3 = 10,25 * min;
Level4 = 9.08 * min;
Level5 = 7.91 * min;
Level6 = 6,83 * min;
Level7 = 5,66 * min;
Level8 = 4,5 * min;
Level9 = 3,33;

climb = (Level1 + Level2 + Level3 + Level4 + Level5 + Level6 + Level7 + Level8) * 1.4;
topFly = Level9*time2;

descend = (Level1 + Level2 + Level3 + Level4 + Level5 + Level6 + Level7 + Level8 ) * 0.9;

altlevel = climb + descend + topFly;


}

void Climb_Descend (float altlevel ) {
cout <<altlevel;

}
closed account (3pj6b7Xj)
your using float, float is very high precision and the results are too fine for comparison...I would recommend you use double or int or unsigned int which ever works for ya.
The problem is this:
1
2
3
4
5
void Altitude(float time, float time2, float altlevel) //function
{
  ...
  altlevel = climb + descend + topFly; // no effect
}


Climb_Descend (altlevel); // altlevel remains uninitialized


mrfaosfx wrote:
float is very high precision
Huh?
1
2
    cout << "sizeof(float): " << sizeof(float) << endl;
    cout << "sizeof(double): " << sizeof(double) << endl;
leads to (Windows XP/GCC)
sizeof(float): 4
sizeof(double): 8


EDIT: There are more problems:
1
2
3
4
5
6
7
8
9
Level1 = 12,5 * min; // , instead of .
Level2 = 11,33 * min; // , instead of .
Level3 = 10,25 * min; // , instead of .
Level4 = 9.08 * min;
Level5 = 7.91 * min;
Level6 = 6,83 * min; // , instead of .
Level7 = 5,66 * min; // , instead of .
Level8 = 4,5 * min; // , instead of .
Level9 = 3,33; // , instead of . 
Last edited on
I dont understand, altlevel is intialized.
Last edited on
altlevel is actually not initialized. The data that you see there is garbage. C and C++ don't do any garbage management for you; they don't rewrite memory when you declare a variable. Whatever was in the spot now marked by your new data is what you see if you print the variable.

The problem with altlevel not being initialized is that you didn't send it by reference, so whatever changes you did inside the void Altitude Function are not being carried back to main(). Just change the

void Altitude(float time, float time2, float altlevel)

to
void Altitude(float time, float time2, float& altlevel)
Yeah that works now, one last question how can I add a return value to this program, because I was trying to return altlevel and it doesnt work.
you need to change
 
void Altitude(float time, float time2, float altlevel)
to
 
float Altitude(float time, float time2, float altlevel)
then you can place a return altlevel. But then you need to assign the value to altlevel like this:
altlevel = Altitude(speed, altspeed, altlevel); which then there's no point in sending altlevel over in the first place. The method by adding the & to the function definition sends the variable by reference and changes the value in the calling function. This will be important when you need to deal with functions changing and having to return more than one item.
When it comes to a function just changing one variable you should return a value instead of using the pointer method. This is incase you want to do something like use this as part of the Travelling Salesmen problem and you are saving the altlevel into a vector or array for later comparison. You can actually declare both versions of your Altitude(...) fucntion like this:

1
2
void Altitude(float, float, float&);
float Altitude(float, float);


If you wanted to see the results side by side. This is called Overloading.
Last edited on
Topic archived. No new replies allowed.