0

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:

  1. 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)

  1. 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:

  1. allow for trailing zeros to be included in my output, just like the other outputs?
  2. accurately total the totalmilespergallon
  3. prevent the decimal value from rounding up a digit and keep exact

Thank you in advance for any assistance, as it is truly appreciated!

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
Clarky7782
  • 13
  • 3
  • tl;dr, one question per question please – 463035818_is_not_an_ai Sep 20 '16 at 11:40
  • Closing as a dupe for the first question. – NathanOliver Sep 20 '16 at 11:40
  • @NathanOliver a link to the answered question would be useful. – Kokozaurus Sep 20 '16 at 11:41
  • 2
    @Dr.Nefario It is at the top of the question in the *This question already has an answer here:* banner – NathanOliver Sep 20 '16 at 11:42
  • The answer to the second question: You do `totalgallonsused += gallonsused;` before `if (gallonsused == -1) break;` so you add the last `-1` to the total. – alain Sep 20 '16 at 11:43
  • [boost::format](http://www.boost.org/doc/libs/1_61_0/libs/format/) might also be useful – sp2danny Sep 20 '16 at 11:47
  • thank you, alain! I placed it after the break and it worked like a charm. Very much appreciated! Any ideas on what I'm missing with the trailing zeros not adding and the value rounding up? If I need to do a new question, I totally understand and will do so. – Clarky7782 Sep 20 '16 at 11:54
  • You're welcome. Did you read the question and answers from the link in the yellow box at the top? It should answer the "trailing zeros" question. – alain Sep 20 '16 at 12:18

0 Answers0