Looking better in quite a few places!
I'm afraid this doesn't fix the problem. AddrInfo.good() checks that there's no error and no EOF. So on the last valid run through the loop, getline fetches the last line. That doesn't set EOF, because it hasn't tried to read any further yet.Code:AddInfo >> lineBuffer;
while(AddInfo.good())
{
getline(AddInfo, lineBuffer, '\n');
string *theFields = split(lineBuffer, ',');
cout << "SHOW RECORDS" << endl;
cout << "---------------------------------" << endl;
cout << "Name............ " << theFields[0] << endl;
cout << "Street Address.. " << theFields[1] << endl;
cout << "City............ " << theFields[2] << endl;
cout << "State........... " << theFields[3] << endl;
cout << "Zip Code........ " << theFields[4] << endl;
cout << "---------------------------------" << endl;
}
The loop will execute again -- getline will put the empty string "" in lineBuffer. split() will see that there are no commas, so only one (empty) substring.
Your function then tries to read from theFields[1] and onwards, but these don't exist. Hence crash. The loop needs to exit as soon as EOF is encountered by getline.
Really your code shouldn't be assuming that the array is a particular length. If all goes well, it will be, but if all does not go well, it's not ok for the program to crash. It'd be better to have split() also return the number of elements in the array. You could error and gracefully exit if it's not 5. To return an extra value from split() you could pass a count int as a reference.
If you were using vectors, you could just check vec.size().
You also still have this line before the loop:
This will just read the first word in the file, which could also crash your program depending on the contents of the file.Code:AddInfo >> lineBuffer;
Just a style comment
It'd be nicer to use a relative path here. Best would be to store TestAddress.txt alongside the executable, or in a directory next to the executable. Then the program could be run on different machines and would work correctly.Code:ifstream AddInfo("C:\\Users\\User\\Documents\\Visual Studio 2012\\Projects\\Lab07\\Lab07\\TestAddress.txt", ios::in);
If you want to pick it up from your source dir though I think it's
Code:..\\Lab07\\TestAddress.txt