954,167 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

figure this out

well this is what my program does:

//asks for input for number of gallons
enter the gallons of gas used (-1 to end):

//asks for input for number of miles
enter miles driven:

//then shows the miles / gallon for this tank
the miles / gallon for this tank was ...

it loops this untill the user presses -1
then i want it to find out the avg of all the miles/gallons.

problem is that avg is over written every time the loop starts
here is the code for the program.

#include
#include

float gal;
float avg;
void gas();
//void totalavg();
void main()
{
while(gal>=0)
gas();
getch();
}

void gas()
{
float miles;

cout <<"enter the gallons of gas used (-1 to end): ";
cin >> gal;
if(gal!=-1)
{
cout <<"enter miles driven: ";
cin >> miles;
avg=miles/gal;
cout <<"the miles / gallon for this tank was "<

fakespike
Junior Poster in Training
58 posts since Jul 2003
Reputation Points: 10
Solved Threads: 0
 

Are you still working on this or did you figure it out?

Bob
Junior Poster
Team Colleague
129 posts since Feb 2003
Reputation Points: 15
Solved Threads: 2
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You