E
Eric Whittaker
Hey guys, i am relatively new at this programming thing and am stuck on an
assignment here. I'm trying to get the program below to respond to the
integer -1 to stop and calculate total. It does, but it makes me enter -1
under both gallons and miles, how can i avoid this? Thanks.
//Figures gas mileage in gallons
#include <iostream>
#include <conio.h>
using std::cout;
using std::cin;;
using std::fixed;
#include <iomanip>
using std::setprecision;
// function main begins program execution
int main()
{
int gal; //number of gallons used
int miles; //number of miles driven
int tg; //total of all gallons used
int tm; //total of all miles driven
double mpg; //miles per gallon
double total; //average of all drives
//initialization phase
tg = 0;
tm = 0;
while ( gal != -1 ) {
cout << "\nEnter the gallons used (Enter -1 to finish) :\n";
cin >> gal;
cout << "Enter the miles driven:\n";
cin >> miles;
tg = tg + gal;
tm = tm + miles;
//calculate miles per gallon
mpg = static_cast < double > ( miles ) / gal;
cout << "The per gallon for this trip were :\n" << setprecision ( 6 ) <<
fixed << mpg;
}
if ( gal != 0 )
total = static_cast < double > ( tm ) / tg;
cout << "\nThe overall average was:\n";
cout << total;
;return 0;
}
assignment here. I'm trying to get the program below to respond to the
integer -1 to stop and calculate total. It does, but it makes me enter -1
under both gallons and miles, how can i avoid this? Thanks.
//Figures gas mileage in gallons
#include <iostream>
#include <conio.h>
using std::cout;
using std::cin;;
using std::fixed;
#include <iomanip>
using std::setprecision;
// function main begins program execution
int main()
{
int gal; //number of gallons used
int miles; //number of miles driven
int tg; //total of all gallons used
int tm; //total of all miles driven
double mpg; //miles per gallon
double total; //average of all drives
//initialization phase
tg = 0;
tm = 0;
while ( gal != -1 ) {
cout << "\nEnter the gallons used (Enter -1 to finish) :\n";
cin >> gal;
cout << "Enter the miles driven:\n";
cin >> miles;
tg = tg + gal;
tm = tm + miles;
//calculate miles per gallon
mpg = static_cast < double > ( miles ) / gal;
cout << "The per gallon for this trip were :\n" << setprecision ( 6 ) <<
fixed << mpg;
}
if ( gal != 0 )
total = static_cast < double > ( tm ) / tg;
cout << "\nThe overall average was:\n";
cout << total;
;return 0;
}