I am attempting to create a gas\miles driven\MPG log that loops until the sentinel value of -1 is entered. It then provides the accumulated totals for gallons used, miles driven, and overall avg. MPG. Here is my code:
int main(void)
{
StartUp(); //indicates beginning of code
double startmileage = 0; //indicates starting mileage, initialized at the value of 0
double endmileage = 0; //indicates ending mileage, initialized at the value of 0
double milesdriven = 0; //indicates total miles driven, initialized at the value of 0
double gallonsused = 0; //indicates total gallons used, initialized at the value of 0
double milespergallon = 0; //indicates total miles per gallon, initialized at the value of 0
double totalgallonsused = 0;
double totalmilesdriven = 0;
double totalmilespergallon = 0;
while (true)
{
cout << "Enter the gallons used (-1 to end): "; //prompts user to enter total number of gallons used for trip
cin >> gallonsused; //user enters data
totalgallonsused += gallonsused;
if (gallonsused == -1)
{
break; // Check for sentinel value of -1
}
cout << "Enter the starting mileage: "; //prompts user to enter start mileage for trip
cin >> startmileage; //user enters data
cout << "Enter the ending mileage: "; //prompts user to enter end mileage for trip
cin >> endmileage; //user enters data
milesdriven = endmileage - startmileage; //calculates total miles driven
cout << "Miles driven: " << milesdriven << endl; //outputs total miles driven to screen
totalmilesdriven += milesdriven;
milespergallon = milesdriven / gallonsused; //calculates total miles per gallon
cout << setprecision(8);
cout << "The miles / gallon for this tank was: " << milespergallon << endl; //outputs miles per gallon to screen
totalmilespergallon = totalmilesdriven / totalgallonsused;
cout << "\n\n" << endl;
}
// Show grand totals
cout << "\n\nGrand totals: " << endl;
cout << "Total gallons used: " << totalgallonsused << endl;
cout << "Total miles driven: " << totalmilesdriven << endl;
cout << "The overall average miles/gallon: " << totalmilespergallon << endl;
cout << "\n\n" << endl;
WrapUp(); //indicates end of code
return Success;
}
While I am able to run the program, I am having 2 issues with the output:
- Trailing zeros are not being kept for milespergallon (example: reads as 24 when should be 24.000000) . I've tried placing a set precision with the miles per gallon, but it still does not leave the zeros, though it leaves other values (example: will output 22.421875 when adding cout << setprecision (8). However, 24 remains 24)
EXAMPLE:
Enter the gallons used (-1 to end): 5 Enter the starting mileage: 487 Enter the ending mileage: 607 Miles driven: 200 The miles / gallon for this tank was: 24 (SHOULD BE 24.000000)
- Totalgallonsused does not appear to be adding correctly and is deducting 1 from the total. For instance:
I enter the following information (I also added another note with a rounding issue on a decimal value):
Enter the gallons used (-1 to end): 12.8 Enter the starting mileage: 0 Enter the ending mileage: 287 Miles driven: 287 The miles / gallon for this tank was: 22.421875
Enter the gallons used (-1 to end): 10.3 Enter the starting mileage: 287 Enter the ending mileage: 487 Miles driven: 200 The miles / gallon for this tank was: 19.417476 (PLEASE NOTE: This should be 19.417475. Not sure why this is rounding. How would I get this exact?)
Enter the gallons used (-1 to end): 5 Enter the starting mileage: 487 Enter the ending mileage: 607 Miles driven: 200 The miles / gallon for this tank was: 24
Enter the gallons used (-1 to end): -1
Grand totals: Total gallons used: 27.1 (****SHOULD BE 28.1****) Total miles driven: 607 The overall average miles/gallon: 21.601423
How would I:
- allow for trailing zeros to be included in my output, just like the other outputs?
- accurately total the totalmilespergallon
- prevent the decimal value from rounding up a digit and keep exact
Thank you in advance for any assistance, as it is truly appreciated!