0
#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 by Dani: Formatting fixed

3
Contributors
4
Replies
5
Views
14 Years
Discussion Span
Last Post by yogesh
0

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().

0

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

0

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.

0

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 by Dani: Formatting fixed

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.