extra iteration in for loop when reading from text file

The purpose of this little project is to read from a file an address book entry, which includes name, DOB, address, etc... All entries are read correctly, but then one final iteration takes place in error. I need answers to the following questions:
1. Why is my loop iterating an extra time?
2. is it ok to declare variables in each iteration of the loop for this purpose?
3. am I reading each piece of information correctly?


Here is a modified version of the file to be read ...

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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
John Doe
07 03 1985
7313 magical ln
Chattanooga
TN
37412
423-123-4566
m
James Dobson
06 26 1929
10176 fake ln
Collegedale
TN
37363
423-911-9414
m
The Zohan
07 02 1991
1507 Crazy ln
Chattanooga
TN
37412
423-555-1111
m
Tony Dubya
06 23 1985
10176 swimsicle drive
collegedale
Tn
37363
423-991-9919
f
Kim Kimbers
01 01 1961
9405 Hixon Pk
Chattanooga
TN
37412
423-913-9999
m
Shannon Cathy
02 08 1986
9405 Hixon Pk
Chattanooga
TN
37412
423-998-9802
m


The code...

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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
void addressBookType::getAddrBook(string fileName)
{
	ifstream inData;
	inData.open(fileName.c_str());



		for (int i=0; !inData.eof(); i++)
		{

			string strFname,
			strLname,
			strStr, 
			strCt, 
			strSt, 
			strZp, 
			strMo, 
			strDy, 
			strYr, 
			strPhn, 
			strRel;	
			
			inData >> strFname;
			inData >> strLname;
			inData >> strMo;
			inData >> strDy;
			inData >> strYr;
			inData.get();
			getline(inData, strStr);
			getline(inData, strCt);
			getline(inData, strSt);
			getline(inData, strZp);
			getline(inData, strPhn);
			inData >> strRel;
			addrList[i].setName(strFname, strLname);
			addrList[i].setAddress(strStr, strCt, strSt, strZp);
			addrList[i].setDOB(strMo, strDy, strYr);
			addrList[i].setPhone(strPhn);
			addrList[i].setRel(strRel);
			addrList[i].print();
			addrIndex = i;


			
			
		}


	// opens ifstream variable inData
	// pulls all entries to addrList[] (500 max)
	// count all entries and store value in addrIndex
inData.close();
}



Here is the results....

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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
John Doe
07 03 1985
7313 magical ln
Chattanooga
TN
37412
423-123-4566
m
James Dobson
06 26 1929
10176 fake ln
Collegedale
TN
37363
423-911-9414
m
The Zohan
07 02 1991
1507 Crazy ln
Chattanooga
TN
37412
423-555-1111
m
Tony Dubya
06 23 1985
10176 swimsicle drive
collegedale
Tn
37363
423-991-9919
f
Kim Kimbers
01 01 1961
9405 Hixon Pk
Chattanooga
TN
37412
423-913-9999
m
Shannon Cathy
02 08 1986
9405 Hixon Pk
Chattanooga
TN
37412
423-998-9802
m
Name:  
DOB: --
Street: 
City: 
State: 
Zip: 
Phone: 
Relationship: 
Might there be an extra empty line after the last non-empty line? empty line won't trigger eof()
for (int i=0; !inData.eof(); i++) You don't get eof, until you try to read it.
Suppose that you have a file that only has 1 line. The eof flag will not set when you read that line, but after you try to read again.

2. There is no problem in declaring the variables inside the loop. If you are worried about creation/destruction the compiler may optimize that.
However you should encapsulate more.
1
2
3
contact Contact;
while( inData>>Contact )
  List.add( Contact );


3. ¿Why is everything a string?

By the way, you should drop the hungarian notation. ¿What the hell is 'strStr' or 'strSt' ?
Thanks guys for your quick replies; I really appreciate your time!

I'm using all strings so I wont have to do any type casting (eg. char to string, string to char, int to string, etc...)
I suppose for a more complex program it would be more efficient to do otherwise, but for now I'm not too concerned about that.

I've been using str as a prefix to indicate string. strStr is to hold the street address. strSt is for state.

The last line of the file is the part of an entry that indicates relationship status. So there are no blank lines after the last part of the last entry. So after the last entry has been read, there eof should be set to true, so the loop should terminate immediately, correct?
ne555 is correct, you need to read the eof

overall layout should look like this

1
2
3
4
5
6
7
8
9
10
read first line of record
for(int i=0; !inData.eof(); i++)
{
   
    read rest of record

    process data

    read first line of record
}


something like that

The last line of the file is the part of an entry that indicates relationship status. So there are no blank lines after the last part of the last entry. So after the last entry has been read, there eof should be set to true, so the loop should terminate immediately, correct?

Seemly it depends on C++ library implementation. On Windows, after the last entry is read, eof() is set, but on Linux, it's not, so you have to read again to workaround this difference.
Ok I see now. Thanks so much guys!
Topic archived. No new replies allowed.