#include <iostream.h>
#include <conio.h>


void gas();
void main()
{
    float gal;
    while(gal<=0)
    gas();

    cout<<"thanks for your help";
        getch();
}

void gas()
    {
        float gal;
        float total;
        float miles;
        float avg;
        cout <<"enter the gallons of gas used (-1 to end): "; 
        cin  >> gal;
        cout <<"enter miles driven: ";
        cin  >> miles;
        avg=miles/gal;
        cout <<"the miles / gallon for this tank was "<<avg<<endl;
        cout<<"\n";
    }

------------------------------------------------------------------------------
i'm tring to get float gal value to be shared between gas and main.
so that if -1 is enter for gal the while loop will exit.
help would be aprecaited.

Edited 3 Years Ago by Dani: Formatting fixed

You can either make gal a global variable, or pass rewrite gas() so that it receives a pointer to a integer. Then pass gal to gas().

i feel stupid now that the sulotion was so simple thanks for ur help that answered my question

i have faced some problems.
1)when -1 is pressed it doesnt exit immediately it finishes the function gas() first.
2)i have to then find the average of all the averages entered. but avg varible over writes its result. so correct me if i'm wrong but wont i have to create a array for avg.

then i have to present the average of avg for the passed serveral cars when -1 is pressed.

i know its a bit confusing so help would be greatly appreciated. sorry for my ignorance i'm still new and learning.

declare variable gal as global variable ( out of both the function above main() ) , remove local declaration of gal from both the function. you will be able to share same gal betn main and gas. I am showing you code snap

float gal = 0.0f;

main()
{
    while( gal >= 0.0f)
    {
         gas()
    }

}

gas()
{
   // dont declare gal again in this function
   ....
   cin<<gal;  // same global gal 
}

Edited 3 Years Ago by Dani: Formatting fixed

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