I am writing a banking program to practice my c++ skills, and I am stuck on reading/writing from/to a file. I am storing some basic info (name, account number, pin, balance) to a file, and want to be able to read from that file anytime I open the program. I made a simple class called client:
class client
{
public:
int acctNum;
string firstName;
string lastName;
float balance;
int pin;
}
I save the info to a txt file formatted by account number, pin, first name, last name, and balance:
1 88 Doug Dimmadome 5
My loop over the input file only reads in the account number though, and continuously does so with no end (inFile is the variable name for my ifstream, and clientList is my variable name for an array of clients):
while (!inFile.eof()) {
inFile >> acc;
cout << acc << endl;
clientList[acc].acctNum = acc;
inFile >> clientList[acc].pin;
inFile >> clientList[acc].firstName;
inFile >> clientList[acc].lastName;
inFile >> clientList[acc].balance;
}
What am I doing wrong in my loop so that it doesn't properly loop over all the data in the file?
------EDIT------ I'm adding my variable declaration and opening my file:
client clientList[maxClients];
client currTrans;
int choice = NULL;
char atm = 'y';
ifstream inFile;
ofstream file;
const string fileName = "clientList.txt";
int acc;
inFile.open(fileName);
if (!inFile.is_open()) {
cout << "File could not open.";
return 1;
}
And my client constructor:
client::client() {
acctNum = NULL;
firstName = "";
lastName = "";
balance = NULL;
pin = NULL;
}