0

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;

}

Ya_Boi
  • 13
  • 5
  • 1
    I don't see where you create `clientList[acc]`... What is the type of clientList? Did you try debugging? What is happening instead of what you expect? Do you have people with more than 2 names in the file? – xception Dec 11 '19 at 14:36
  • Most likely this is a result of a failure of properly checking for the input stream's error state. What does your debugger show? If you don't know how to use a debugger this is a good opportunity to learn how to use it to run your program one line at a time, and seeing what happens. Knowing how to use a debugger is a required skill for every C++ developer, no exceptions. With your debugger's help you should be able to quickly find all bugs in this and all future programs you write, without having to ask anyone for help. – Sam Varshavchik Dec 11 '19 at 14:37
  • @Ya_Boi can you also add the content of your array clientList and how you created the ifstream inFile to the question please? – octopus Dec 11 '19 at 14:47
  • 2
    Please read [while (!inFile.eof()) considered wrong](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons) – PaulMcKenzie Dec 11 '19 at 14:48

1 Answers1

0

I think I figured it out through a combination of @Sam Varshavchik and @PaulMcKenzie:

  • I used the debugger to figure out where my program was failing, it was continuously reading the same line so it had nowhere to exit the loop.
  • I changed how I was detecting the end file by listing the account number twice and checking to make sure that a new piece of data was read on each loop:

    while (**inFile >> beg**) {
        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;
    }
    

So now my data in my text file looks like this:

1
1 22 Doug Dimmadome 300
2
2 66 Dif Difadif 400
34
34 123 Mike Wasowski 666
44
44 67 Dale Dalers 600
99
99 44 Mitch Turtle 0.02
Ya_Boi
  • 13
  • 5