File not loading correctly when program starts

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.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
//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 


Any help will be much appreciated.

Thanks
Last edited on
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.

Also, my sample code should read


ldata >> one[index].actNum;
ldata >> one[index].actNam;
ldata >> one[index].balance;
Last edited on
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.
Untested, but should give you the idea:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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.
}


[edit: stray is to in]
Last edited on
Thank you for the help, I appreciate it immensely. I tried applying the following changes as you've thoughtfully put together.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
//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.

1
2
3
	if ( ldata ) 
				index ++;
				count++;


was missing

1
2
3
4
	if ( ldata ) {
				index ++;
				count++;
} //brackets!! 


I'll keep running tests, but I'm pretty sure it's fixed now.

Thank you so much!!!!!
Last edited on
The only difference between a struct and a class in C++ is the default access level (public in struct, private in class.)

The destructor I was referring to was that of in in my code or ldata in yours.

You need to change lines 25 to 27 in your latest code to:

1
2
if ( ldata )
    index++, count++ ;

or:

1
2
3
4
5
if ( ldata )
{
    index++ ;
    count++ ;
}


What you have written is the same as:

1
2
3
4
if ( ldata )
    index++ ;

count++;


so count is incremented regardless of whether or not the extraction was successful.

Last edited on
1
2
3
4
5
6
7
8
while(
   count<LIST
   and in
      >>one[count].actNum
      >>one[count].actNam 
      >>one[count].balance
)
   ++count;
Topic archived. No new replies allowed.