I have a text file called scores.txt with the following data:
John T Smith 90
Eric K Jones 103
Ram S Krishnan 25
Marie A Bell 50
I am trying to read the data from this file and print it using the following C++ code:
ifstream input;
input.open("scores.txt");
string firstName;
char mi;
string lastName;
int score;
while (input) {
input>>firstName>>mi>>lastName>>score;
cout<<firstName<<" "<<mi<<" "<<lastName<<" "<<score<<endl;
}
input.close();
cout<<"Done"<<endl;
The output is:
John T Smith 90
Eric K Jones 103
Ram S Krishnan 25
Marie A Bell 50
Marie A Bell 50
Why is the last line (Marie A Bell 50) being printed twice? How can I prevent this from happening?