while loop-- NEED HELP

I need help on how to write a while loop for this program (the assignment is posted below). I have no idea how to set it up other than what I have in the code area of this. Thanks in advance for any tips or help!



Programming Assignment:
Car MileageYou are to write a program to calculate the average miles per gallon driven over a period of time. The input file contains of an unknown number of lines of the form odometer-reading gallons-pumped.

Here’s a sample file:
29092 12.3
29412 11.9
29805 10.4
30177 11.75

The output is to the screen and is to look as follows (for the above data file).


Miles Gallons MPG
---------------------
320 11.9 26.9
393 10.4 37.8
372 11.8 31.7

Note that the 12.3, the gallons pumped on the very first line, is not used for anything. (You can imagine that this is the gallons pumped to fill the car tank to start.)



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
  #include <iostream>
#include <fstream>
#include <string>
#include <iomanip>

using namespace std;

int main()
{
    ifstream inFile;
    ofstream outFile;
    string inFileName;

    float odometer;
    float mile;
    float pumped;
    float gal;


    cout << endl;
    cout << "Please enter a file name: ";
    cin >> inFileName;

    inFile.open(inFileName.c_str());

    inFile >> odometer >> mile >> pumped;
    while(inFile)
    {
     inFile >> odometer >> mile >> pumped;


    }
Last edited on
Lines 26, 29: Your input file has only two fields. You're trying to read 3.

You need to special case the first record. You need to initialize the previous odometer reading from the first record.

Once you've read the first record, then you can treat the remaining records in the following manner.
1
2
3
4
5
6
7
8
9
10
11
12
  //  Read the first record
  if (! (inFile >> odometer >> pumped))
  {  cout << "Could not read first record" << endl;
      return 1;
  }
  //  Read the remaining records
  while (inFile >> mile >> pumped)
  {  miles_traveled = mile - odometer;
      odometer = miles;    // for next iteration
      mpg = miles_traveled / gal;
      cout << milles_traveled << gal << mpg << endl;
  }
Last edited on
First, thank you so much for your response and help! Here is the upgraded code... When I compile it, it sends back this error message: mileage.cpp:29: error: invalid operands of types ‘bool’ and ‘float’ to binary ‘operator>>’

I am not really sure how to solve this issue. I am extremely new to programming, so I am not familiar with most things.


cout << endl;
cout << "Please enter a file name: ";
cin >> inFileName;

inFile.open(inFileName.c_str());


if (! inFile >> mile >> pumped);
{
cout << "Could not read first file" << endl;
return 1;
}

while(inFile >> mile >> pumped)
{
milesTraveled = mile - odometer;
odometer = mile;
mpg = milesTraveled / gal;

cout << fixed << setprecision(2) << milesTraveled << gal << mpg << endl;
}


Last edited on
Two problems with the following statement.
 
if (! inFile >> mile >> pumped);

1) The ; terminates the if statement. The subsequent lines are executed unconditionally.
2) () are needed around the input operation:

 
if (! (inFile >> mile >> pumped))


Note that in the example I gave you, the first read was to the odometer variable (i.e. previous mileage reading). If you don't do that, you need to move miles to odometer before you enter the loop.

Please ALWAYS use code tags.

Topic archived. No new replies allowed.