0

Currently working on the following homework problem:

Enter destination city, miles traveled to get there and gallons of gasoline used for any number of trips entered at the keyboard (use ctl+z to stop). Use a function to compute miles per gallon(miles traveled/gallons used). Display the destination city and miles per gallon for each trip entered. Sum the miles traveled and give a count of the number of trips made. Display these at the end of the program.

The problem occurs during the second iteration of the while loop. When asking the using for input, the output "Enter destination city, ctrl+z to stop:" and 'Enter miles to destination and gallons of gasoline needed, ctrl+z to stop:" are merged into one output.

In other words, the console does not allow the user time to input a city after asking for a destination the second time. Instead, both outputs are displayed back to back and whatever is input is stored into gallons and gasoline resulting in an error.

I have tried adding more likes between the code.

I tried adding a system("pause") between the lines.

#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

float mpgFunc(float, float);

int main()
{
    //variable declaration

    string destination;
    float milesToDest, gasolineNeeded, MPG, totalMiles = 0.0f;
    int count = 0;

    //input phase

    cout << "Enter destination city, ctrl+z to stop: " << endl;

    getline(cin, destination);

    cout << "Enter miles to destination and gallons of gasoline needed, 
        ctrl+z to stop: " << endl;

    cin >> milesToDest >> gasolineNeeded;

    while (!cin.eof())
    {

        MPG = mpgFunc(milesToDest, gasolineNeeded); //function call

        count = count + 1; //counter

        totalMiles = totalMiles + milesToDest; //sum

        cout << setprecision(2) << fixed;
        cout << "Destination city: " << setw(10) << destination << 
                endl;
        cout << "Miles per gallon: " << setw(10) << MPG << endl;

    /* Problem

        cout << "Enter destination city, ctrl+z to stop: " << endl << 
                endl;
        getline(cin, destination); 



        cout << "Enter miles to destination and gallons of gasoline 
                needed, ctrl+z to stop: " << endl;
        cin >> milesToDest >> gasolineNeeded;

problem happens here the two outputs are merged into one output */

    }

    cout << endl;
    cout << "Total miles traveled: " << setw(8) << totalMiles << endl;
    cout << "Total trips taken:    " << setw(8) << count << endl;

    system("pause");
    return 0;
}//end main

float mpgFunc(float milesToDest, float gasolineNeeded)
{
    float MPG;

    MPG = milesToDest / gasolineNeeded;

    return MPG;
}
Remy Lebeau
  • 454,445
  • 28
  • 366
  • 620
  • Possible duplicate of [Why does std::getline() skip input after a formatted extraction?](https://stackoverflow.com/questions/21567291/) – Remy Lebeau Sep 30 '19 at 22:20
  • You should read [Why is “while \( !feof \(file\) \)” always wrong?](https://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong) – Shawn Sep 30 '19 at 22:39
  • Thank you for the recommendation! I added a cin.ignore() after the second output which cleared the cin buffer and that solved the issue. – cakeLord Sep 30 '19 at 23:07

0 Answers0