File Lab

Type in Word. Work with a partner and submit ONE COPY per pair. Include the following at the top of your submission:

 

Partner 1:       Name ______________________________________________

role (driver or navigator) _____________________________

 

Partner 2:       Name ______________________________________________

role (driver or navigator)______________________________

 

Please note if one partner did not work on all problems:

---------------------------------------------------------------------------------------------------

Program 1

#include <iostream>

#include <fstream>    // For file I/O

using namespace std;

int main()

{

   

    float    startMileage;

    float    endMileage;

    float    numGallons;      // # of gallons for fillup

    float    totGallons = 0;

    float    mpg;             // Computed miles per gallon

    ifstream inFile;

    ofstream outFile;        

 

    // Open the files

    inFile.open("??:inmpg.txt");//replace ?? with the appropriate path for YOUR file

    if (!inFile)

    {

        cout << "can't find inmpg.txt" << endl;

        return 0;

    }

    outFile.open("??:outmpg.txt");//replace ?? with the appropriate path for YOUR file

 

    // Get data

    cout << "reading from file" << endl;

    inFile >> startMileage >> endMileage;

     inFile >> numGallons;

while (inFile)

{

totGallons = totGallons + numGallons;

infile >> numGallons;

}

 

     // Compute miles per gallon

    mpg = (endMileage - startMileage) / totGallons;

 

    // Output results

    cout << "wrote to file outmpg.txt" << endl;

    outFile << "For a starting mileage of " << startMileage << endl;

    outFile << "and an ending mileage of " << endMileage << endl;

    outFile << "the mileage per gallon is " << mpg << endl;

     outFile.close();

     inFile.close(); 

    return 0;

}

1.    Type in the above program as mileage.cpp. Compile.

2.    Create a text file called inmpg.txt. It should contain the starting and ending miles on your odometer, then four gallon amounts.  This file should contain no text, only numeric values separated by blanks.


67308.0 68750.5 11.7 14.3 12.2 8.5

 

 

3.    Run the program and list the output file (open outmpg.txt).

4.    Create a new input file. You’ll have to change the name of the file in your program. Be sure to change the name of your output file as well, so you don’t write over your previous work. The new file will have only odometer readings – no gallons.

 

42003 42003

 

5.    Run.  Change the program so it works correctly and run again. Ensure that the program still functions with the original input.

Submit the final program, both input files and both output files.