It took me a few minutes of reading and rereading your comments smokey before it I got it, but I finally found the problem..
The code is now working as it should and more importantly I understand why it's working now. Thanks again for everyone's help.
For kicks, here's the final version.
Code:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
void menu(void);
void writeData(void);
void readData(void);
string * split(string theLine, char theDeliminator);
const char FileName[] = "TestAddress.txt";
int main ()
{
menu();
return 0;
}
void menu(void)
{
char answer;
do
{
cout << "Welcome to the Address Database." << endl;
cout << "Do you wish to (A)ppend records, (D)isplay records, or (E)xit the program?" << endl;
cin >> answer;
cin.ignore();
switch (answer)
{
case 'A':; case 'a':;
writeData();
break;
case'D': case 'd':;
readData();
break;
case'E': case 'e':;
exit (EXIT_SUCCESS);
break;
default:
cout << "Invaild selection" << endl;
}
}
while ((answer != 'E') || (answer != 'e'));
}
void writeData(void)
{
char answer;
ofstream AddInfo("TestAddress.txt",ios::app);
if (AddInfo.is_open())
{
do
{
cout << endl;
cout << "First and Last Name : " << endl;
string name;
getline(cin, name, '\n');
cout << endl;
cout << "Street Address: " << endl;
string address;
getline(cin, address, '\n');
cout << endl;
cout << "City: " << endl;
string city;
getline(cin, city, '\n');
cout << endl;
cout << "State: " << endl;
string state;
getline(cin, state, '\n');
cout << endl;
cout << "Zip Code: " << endl;
string zip;
getline(cin, zip, '\n');
AddInfo << name << "," << address << "," << city << "," << state << "," << zip << endl;
cout << endl;
cout << "Enter Another Record? (Y/N)" << endl;
cin >> answer;
if (answer == 'N' || (answer == 'n'))
{
menu();
}
cin.ignore();
}
while (answer == 'Y' || (answer == 'y'));
}
else
{
cout << "Could not open file." << endl;
}
AddInfo.close();
}
void readData(void)
{
int recordCount = 0;
string lineBuffer;
ifstream AddInfo("TestAddress.txt", ios::in);
if (AddInfo.is_open()) // tests to see if file is open
{
cout << endl;
cout << "SHOW RECORDS" << endl;
cout << "------------" << endl;
getline (AddInfo, lineBuffer); //gets first line of text and places it in "lineBuffer"
while(AddInfo.good()) //while file is error free and not EOF
{
string * theFields = split(lineBuffer, ',');
recordCount++;
cout << "\n Record No. " << recordCount;
cout << endl;
cout << "Name............." << theFields[0] << endl;
cout << "Address.........." << theFields[1] << endl;
cout << "City............." << theFields[2] << endl;
cout << "State............" << theFields[3] << endl;
cout << "Zip.............." << theFields[4] << endl;
cout << "------------------------------------------" << endl;
getline(AddInfo, lineBuffer); //gets next line of text into "lineBuffer"
}
AddInfo.close();
menu();
}
else
{
cout << "Unable to open file." << endl;
}
}
string * split(string theLine, char theDeliminator)//Useage: string *theFields = split(lineBuffer, ',');
{
int splitCount = 0;
for(int i = 0; i < theLine.size(); i++)
{
if (theLine[i] == theDeliminator)
splitCount++;
}
splitCount++;
string* theFieldArray;
theFieldArray = new string[splitCount];
string theField = "";
int commaCount = 0;
for(int i = 0; i < theLine.size(); i++)
{
if (theLine[i] != theDeliminator)
{
theField += theLine[i];
}
else
{
theFieldArray[commaCount] = theField;
theField = "";
commaCount++;
}
}
theFieldArray[commaCount] = theField;
return theFieldArray;
}