The following function is suppose to load accounts.txt file into the programs memory when the program executes.
LIST is a global defined const int LIST = 50
Everytime my program runs, this function seems to add an extra account in the form of various numbers.
Example. I have 9 accounts in my accounts.txt file. When the program runs, if I check to see how many accounts have loaded... it tells me 10 accounts have loaded. Where is the extra tenth coming from? When I check accounts.txt it has all of my old accounts in order, but now has an extra account in the form of various numbers at the bottom - making a tenth account.
Summary: my load function is adding an extra account to the accounts.txt file everytime my program executes.
//function: load data file
void loadAct(account one[], int& count){
fstream ldata;
ldata.open("accounts.txt");
if (ldata.fail()){
cerr << "Error opening file. accounts.txt could not be found, make sure its in folder before running program." << endl;
system("PAUSE");
exit(1);
}
for (int index = 0; index < LIST && !ldata.eof(); index++){
ldata >> one[index].actNum;
ldata >> one[index].actNam;
ldata >> one[index].balance;
count++;
}//for
ldata.close();
}//void
Thanks for the fast reply, but that post somewhat confuses me a little more. I've tried while loops in the suggested manner of the post, but just run into other errors. I guess my main problem is not understanding how the code should be coded.
I also noticed that if I change LIST to just 8 - it reads the 8 accounts perfectly. This caused me to add an additional counter my saveAct function (basically reads all the data back into the accounts.txt file upon program shut down). I was hoping I could count how many files are saved into the accounts.txt file - this would enable me to replace LIST with sCount.
example
for (int index = 0; index < sCount && !ldata.eof(); index++)
However, my sCount value is lost once you exit the program, so by default it's always zero every time the program loads.
void loadAct(account one[], int& count){
count = 0 ;
std::ifstream in("accounts.txt") ;
if ( !in.is_open() )
std::cerr << "Error opening file \"accounts.txt\"\n" ;
while ( count < LIST && in )
{
account& a = one[count] ;
in >> a.actNum >> a.actNam >> a.balance ;
if ( in ) // Did we succeed at extracting an account?
++count ; // Yes, increase the number of accounts.
}
// No need to close the ifstream explicitly, the destructor does that for us.
}
//function: load data file
void loadAct(account one[], int& count){
fstream ldata;
int index = 0;
ldata.open("accounts.txt");
if (ldata.fail()){
cerr << "Error opening file. accounts.txt could not be found, make sure its in folder before running program." << endl;
system("PAUSE");
exit(1);
}
while ( index < LIST && ldata ){
//for (int index = 0; index < LIST && !ldata.eof(); index++){
account& a = one[index];
ldata >> a.actNum;
ldata >> a.name;
ldata >> a.balance;
//ldata >> one[index].actNum;
// ldata >> one[index].name;
// ldata >> one[index].balance;
if ( ldata )
index ++;
count++;
}
// }//for
ldata.close();
}//void
Still end up getting the same result, one extra account is loaded.
account is a struct - do structs have destructors? I thought destructors were apart of class.
Actually, I believe it does work now! It was my syntax around the if statement that was causing it to not function correctly after applying your changes.