-5

I am using visual studios and have this program that takes data from a txt file called inmpg and then is supposed to print out to a generated file called outmpg.txt. The code has no erros but when the console window comes up it only gets to "Reading from file" and does not go on from there. "outmpg.txt" is created but blank. Any help is appreciated.

#include <iostream> 
#include <fstream> // For file I/O 
using namespace std;
int main()
{
float amt1; // # of gallons for fillup 1 
float amt2; // # of gallons for fillup 2 
float amt3; // # of gallons for fillup 3 
float amt4; // # of gallons for fillup 4 
float startMiles; // Starting mileage 
float endMiles; // Ending mileage 
float mpg; // Computed miles per gallon 
ifstream inMPG; // Holds gallon amts & mileages. Input 
ofstream outMPG; // Holds miles per gall. Output 
                 // Open the files 
inMPG.open("inmpg.txt");
if (inMPG.fail())
{
    cout << "can't find inmpg.txt" << endl;
    return 0;
}

outMPG.open("outmpg.txt");
if (outMPG.fail())
{
    cout << "can't create/ open outmpg.txt" << endl;
    return 0;
}

// Get data (priming read) 
cout << "Reading from file" << endl;
inMPG >> amt1 >> amt2 >> amt3 >> amt4 >> startMiles >> endMiles;
while (!inMPG.eof())
{
    // Compute miles per gallon 
    mpg = (endMiles - startMiles) / (amt1 + amt2 + amt3 + amt4);
    // Output results 
    cout << "wrote to file outmpg.txt" << endl;
    outMPG << "For the gallon amounts" << endl;
    outMPG << amt1 << ' ' << amt2 << ' ' << amt3 << ' ' << amt4 << endl;
    outMPG << "and a starting mileage of " << startMiles << endl;
    outMPG << "and an ending mileage of " << endMiles << endl;
    outMPG << "the mileage per gallon is " << mpg << endl;
    cout << "\n Reading the next set of data" << endl;
    inMPG >> amt1 >> amt2 >> amt3 >> amt4 >> startMiles >> endMiles;
}
return 0;

}

Rconno
  • 13
  • 2

1 Answers1

0

The problem comes from when you first start reading from the inMPG file. If you only have one entry the loop never executes and will skip over it as it already reached the end of the file. Thus never writing to the outMPG file.

What I did was remove the two lines above the while loop and put them at the beginning inside of the while loop and also removed the two last lines inside the loop.

The other problem is the .eof() call. As to why it doesn't actually work in this scenario is beyond me but I used .peek() instead. So the statement can be changed to while(inMPG.peek() != EOF).

This is the code snippet which worked for me

#include <iostream>
#include <fstream> // For file I/O
using namespace std;
int main()
{
    float amt1; // # of gallons for fillup 1
    float amt2; // # of gallons for fillup 2
    float amt3; // # of gallons for fillup 3
    float amt4; // # of gallons for fillup 4
    float startMiles; // Starting mileage
    float endMiles; // Ending mileage
    float mpg; // Computed miles per gallon
    ifstream inMPG; // Holds gallon amts & mileages. Input
    ofstream outMPG; // Holds miles per gall. Output
    // Open the files
    inMPG.open("inmpg.txt");
    if (inMPG.fail())
    {
        cout << "can't find inmpg.txt" << endl;
        return 0;
    }

    outMPG.open("outmpg.txt");
    if (outMPG.fail())
    {
        cout << "can't create/ open outmpg.txt" << endl;
        return 0;
    }

    while(inMPG.peek() != EOF)
    {
        cout << "Reading the next set of data" << endl;
        inMPG >> amt1 >> amt2 >> amt3 >> amt4 >> startMiles >> endMiles;
        // Compute miles per gallon
        mpg = (endMiles - startMiles) / (amt1 + amt2 + amt3 + amt4);
        // Output results
        cout << "wrote to file outmpg.txt" << endl;
        outMPG << "For the gallon amounts" << endl;
        outMPG << amt1 << ' ' << amt2 << ' ' << amt3 << ' ' << amt4 << endl;
        outMPG << "and a starting mileage of " << startMiles << endl;
        outMPG << "and an ending mileage of " << endMiles << endl;
        outMPG << "the mileage per gallon is " << mpg << endl;

    }
    return 0;
}

My input file was this;

24.0 25.0 23.0 20.0 30.0 50.0
nurtul
  • 274
  • 1
  • 9
  • 19
  • Thank you very much! This was a code provided to use as is and I tried multiple things but not the .peek(). Problem solved thanks again. – Rconno Dec 06 '15 at 02:37
  • _"As to why it doesn't actually work in this scenario is beyond me but I used .peek() instead. So the statement can be changed to while(inMPG.peek() != EOF)"_ Because checking for EOF is **not** how you loop through input in C++. http://stackoverflow.com/q/5605125/560648 – Lightness Races in Orbit Dec 06 '15 at 02:48
  • @LightnessRacesinOrbit Now I get it. Thanks – nurtul Dec 06 '15 at 02:50