Hey guys I'm new to this board, but if you guys could share a little insight I would appreciate it. Now the program is for figuring fuel mileage.
Code:#include <iostream> using std::cout; using std::cin; using std::endl; #include <iomanip> // main function begins int main() { double gal; //gallons used int miles; //miles driven double mpg; //mpg int counter;//counter used double total; //total of miles double average; //overall average total = 0; counter = 0; cout << "Enter the gallons used (-1 to end): "; cin >> gal; cout << "Enter the miles driven "; cin >> miles; mpg = miles / gal; cout << "The miles / gallon for this tank was " << mpg << endl; while ( gal != -1 ) { total = total + gal; counter = counter + 1; cout << "Enter the gallons used (-1 to end) "; cin >> gal; cout << "Enter the miles driven "; cin >> miles; mpg = miles / gal; cout << "The miles / gallon for this tank was " << mpg << endl; } if ( gal != 0 ) { average = ( double) total / counter; cout << "The overall average miles/gallon was " << average <<endl; } else cout << " No data was entered" << endl; return 0; }
Now what happens is, I can input the gallons used and miles travedeled over and over, and it will give me the the miles/gallon answer which is correct. But as soon as I hit -1 to end the program it will continue asking for the gal's used and miles driven. It should terminate right after I hit -1. What have I done wrong? Is it my while statement that is incorrect? I've tried several different ways and nothing works. The program has no errors at all, so it must be a incorrect operator or misplacement.
Thanks