Question: #include #include // For file I/O using namespace std; int main() { float startMileage; float endMileage; float numGallons; // # of gallons for fillup float
#include
#include
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
if (!inFile)
{
cout << "can't find inmpg.txt" << endl;
return 0;
}
outFile.open("??\\outmpg.txt");//replace ?? with the appropriate path
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 and the four gallon amounts. This file should contain no text, only numeric values separated by blanks.
EXAMPLE INMPG.TXT
67308.0 68750.5 11.7 14.3 12.2 8.5
3. Run the program and list the output file (open outmpg.txt). Take screenshot of input and output file, and paste to a word document.
4. In the input file, change starting and ending miles to: 42003 42003
5. Run. Change the program so it works correctly and run again.
Submit the final program, both input files and both output files.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
