C ++ Noob在获得条目总计时遇到问题

时间:2016-09-19 03:00:54

标签: c++

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 };


    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 starting mileage:  ";                                //prompts user to enter start mileage for trip
        cin >> startmileage;                                                //user enters data

        cout << "Enter ending mileage:  ";                                  //prompts user to enter end mileage for trip
        cin >> endmileage;                                                  //user enters data

        milesdriven = endmileage - startmileage;                            //calculates total miles driven
        totalmilesdriven += milesdriven;

        cout << "Miles driven:  " << milesdriven << endl;                   //outputs total miles driven to screen

        milespergallon = milesdriven / gallonsused;                         //calculates total miles per gallon

        cout << "The miles / gallon for this tank was:  " << milespergallon << endl;            //outputs miles per gallon to screen

        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 verage miles/gallon: " << milespergallon <<endl;

    cout << "\n\n" << endl;



WrapUp();

以下是输出:

Enter the gallons used (-1 to end):  12.8
Enter starting mileage:  0
Enter ending mileage:  287
Miles driven:  287
The miles / gallon for this tank was:  22.4219



Enter the gallons used (-1 to end):  10.3
Enter starting mileage:  287
Enter ending mileage:  487
Miles driven:  200
The miles / gallon for this tank was:  19.4175



Enter the gallons used (-1 to end):  5
Enter starting mileage:  487
Enter ending mileage:  607
Miles driven:  120
The miles / gallon for this tank was:  24



Enter the gallons used (-1 to end):  -1


Grand totals:
Total gallons used:  27.1
Total miles driven: 607
The overall average verage miles/gallon: 24



Program Program2.cpp ended successfully.
Press any key to continue . . .

1 个答案:

答案 0 :(得分:0)

每次循环播放都会替换milesdriven。相反,你应该积累它:

milesdriven += endmileage - startmileage;

在循环之后,您不需要计算每加仑英里数。但是你需要计算totalgallonsused

totalgallonsused += gallonsused;

然后循环:

milespergallon = milesdriven / totalgallonsused;