inFile loop problem

Hello,

Im using a inFile loop to read a .txt file everything is good until the output which reads the last line twice. Here is my code:
//==========================================================
//
// Title: lab12-2
// Course: CSC 1101
// Lab Number: lab12-2
// Author: Kevin Russell
// Date: 2/29/2020
// Description:
// A C++ application that reads records fro the Detroit Tigers team.
//
//==========================================================
#include <conio.h> // For function getch()
#include <cstdlib> // For several general-purpose functions
#include <fstream> // For file handling
#include <iomanip> // For formatted output
#include <iostream> // For cin, cout, and system
#include <string> // For string data type
using namespace std; // So "std::cout" may be abbreviated to "cout"

int main()
{
// header
cout << "Welcome to Tigers Tales" << endl;
cout << "-------------------------" << endl << endl;

//variables
ifstream inFile;
int year;
string place;
int wins;
string line;
int losses;
double percentage;
int linecount;
string filename = "TigersRecord.txt";
cout << setprecision(3) << fixed;
int w1 = 10;
int w2 = 8;


inFile.open(filename);
if (!inFile.is_open())
cout << "Error: unable to open file" << endl;
else
{
cout << "Reading tokens from file '" << filename << "'..." << endl<< endl;

getline(inFile, line);
cout << line << endl;
linecount = 1;

while (!inFile.eof())
{
inFile >> year >> place >> wins >> losses;
percentage = wins / ((double) wins + losses);
cout << setw(w2) << left << year << setw(13) << right
<< place << setw(7) << right << wins << setw(w1)
<< losses<<setw(9)<<percentage<<endl;
linecount = linecount + 1;

}
inFile.close();
cout << endl<<linecount << " line(s) read from file '" << filename << "'." << endl << endl;
}
cout << "End of Tigers Tales" << endl;

_getch();

}

any help would be appreciated.
Thanks.
In general, don't loop on eof().

Try looping on the success of the extraction of data itself.

1
2
3
4
5
while (inFile >> year >> place >> wins >> losses)
{
    percentage = wins / (...);
    // ...
}
Thank you so much, Ganado!!
I appreciate it dude, you're a wizard.
Topic archived. No new replies allowed.