Hi,
I am developing a program that should calculate the miles per gallon obtained from tankful. After processing all the input data, the program should calculate the combined miles per gallon obtained for all tankfuls.
So, this is what the output should look like -
Enter the gallons user( -1 to end): 12.8
Enter the miles driven: 287
The miles / gallon for this tank was 22.421875
Enter the gallons user( -1 to end): 10.3
Enter the miles driven: 200
The miles / gallon for this tank was 19.417475
Enter the gallons user( -1 to end): -1
The overall average miles/gallon was 21.601423
This is the code Iv'e done so far. It doesn't give the desired result, as it, does like an endless loop after Iv'e entered the gallons and miles.
Can anyone give me some advice and/or tips please?
Code:#pragma argsused #pragma hdrstop #include<iomanip> #include<iostream> #include<conio> using namespace std; int main(int argc, char* argv[]) { // declare the variables double gallons; // read in the gallons double miles; // read in the miles double totalGallons; // calculate the total gallon input double totalMiles; // calculate the total mile input double total; // calculate the gallons/miles double average; // calculate the average overall double counter; // initialization phase total = 0; // initialise total counter = 1; // initialise loop counter cout << "Enter the gallons used( -1 to end): " << endl; cin >> gallons; cout << "Enter the miles driven: " << endl; cin >> miles; average = miles / gallons; // calculate the the current input cout << average; // display average // loop until sentinal value read from user while ( gallons != -1 ) { total = totalMiles + totalGallons;//add total of gallons to total of miles counter = counter + 1; // increment the counter // prompt for input and read the next gallons cout << "Enter the gallons user( -1 to end): " << endl; cout << "Enter the miles driven: " << endl; } // termination phase // if user entered at least one gallon... if ( counter != 0 ) { // calculate average of all gallons over miles entered average = static_cast< double >( total ) / counter; // display overall average with six digits of precision cout << "Overall average is " << setprecision( 6 ) << fixed << average << endl; } else cout << "No gallons or miles were entered" << endl; getch(); return 0; }