Hello programmers!
I was working on a program that reads a sequential file with bank accounts.
The file has three sets of data per line: the account #, the account holder's name, and balance.
Then, I made a program to print the file- and it printed the last line of the file twice.
Here is the program:
// Reading and printing a sequential file
#include <iostream>
#include <fstream> //file stream
#include <iomanip>
#include <string>
#include <cstdlib>
using namespace std;
void outputLine(int, const string&, double); //prototype
int main()
{
//ifstream construtor opens the file
ifstream inClientFile("clients.txt",ios::in);
//exit program if ifstream could not open file
if (!inClientFile)
{
cerr << "File could not be opened" << endl;
exit(EXIT_FAILURE);
}
int account; //account number
string name; //the account owner's name
double balance; //the account balance
cout << left << setw(10) << "Account" << setw(13) << "Name" << "Balance" << endl << fixed << showpoint;
//display each record in file
while (!inClientFile.eof())
{
inClientFile >> account >> name >> balance;
outputLine(account,name,balance);
}
return 0;
}
//display single record from file
void outputLine(int account,const string& name, double balance)
{
cout << left << setw(10) << account << setw(13) << name << setw(7) << setprecision(2) << right << balance << endl;
}
The file is called "clients.txt". Its content is:
200 nata 266
344 joey 2.44
After seeing this, it is pretty straightforward that the program read the last line twice.
However, I changed the last loop to this, and it worked!
//display each record in file
while (inClientFile >> account >> name >> balance)
{
outputLine(account,name,balance);
}
What was the difference between the old version of the loop and the new version of the loop? Why did the new one work, and the old one did not? Thanks.