hello there all, i am just beginning to learn the C++ language and i am having trouble with this excercise from the book that i am doing:
the code is executing but once i want to end the entering of data through -1 it adds the -1 to the computation.Code:#ifdef HAVE_CONFIG_H #include <config.h> #endif #include <iostream> #include <iomanip> #include <cstdlib> using namespace std; int main() { int totalMile, totalGallon, gallonCounter, aveGallon, aveMile; double milePerGallon, average, mile, gallon, mileGal; totalMile = 0; totalGallon = 0; gallonCounter = 0; cout << "Enter the gallons used, (-1 to end) : " << endl; cin >> gallon; cout << "Enter the miles driven: " << endl; cin >> mile; mileGal = mile / gallon; cout << "The miles / gallon for this tank was: " << setprecision( 2 ) << setiosflags( ios::fixed | ios::showpoint ) << mileGal << endl; while ( gallon != -1) { totalGallon = totalGallon + gallon; totalMile = totalMile + mile; gallonCounter = gallonCounter + 1; cout << "Enter the gallons used, (-1 to end) : " << endl; cin >> gallon; cout << "Enter the miles driven: " << endl; cin >> mile; mileGal = mile / gallon; cout << "The miles / gallon for this tank was: " << setprecision( 2 ) << setiosflags( ios::fixed | ios::showpoint ) << mileGal << endl; } if ( gallonCounter != 0 ) { aveMile = totalMile/gallonCounter; aveGallon = totalGallon/gallonCounter; average = static_cast< double >( aveMile )/static_cast< double >( aveGallon ); cout << "The overall average miles/gallon was: " << setprecision( 2 ) << setiosflags( ios::fixed | ios::showpoint ) << average << endl; } else { cout << "No gallons entered." << endl; } return 0; }
i just want to know what is wrong with my code.
thank you very much.



LinkBack URL
About LinkBacks


