The program should:
Accept a series of names and addresses from the console.
The user's input should be written to a text file in the CSV format described in the lecture. But, do not include the field names in the first row of the file.
Read the records from the text file and display them in a user friendly format.
Provide a menu to allow the user to either append records to the file, display the records or exit the application.
This is what I have. It compiles just fine. I can write to the file but I cannot read the file. Can someone tell me where I went wrong with the code?
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
void menu(void);
void writeData(void);
void readData(void);
string * split(string, char);
const char FileName[] = "c://TestAddress/TestAddress.txt";
ifstream myAddress(FileName);
string name = " ";
string address = " ";
string street= " ";
string city = " ";
string state = " ";
string zipCode = " ";
int record = 0;
ofstream outMyAddress(FileName, ios::out);
int main ()
{
menu();
return 0;
} //end main
void menu(void)
{
//allow user to choose to append records, display records or exit the program
char userChoice = ' ';
//Display Menu
system("cls");
cout << "\nName and Address database:" << endl;
cout << endl;
cout << "\n\n(A)ppend record, (S)how Record, (E)xit:";
cin >> userChoice;
//Users Choice
switch (userChoice)
{
case 'a':
case 'A'://Append Record
myAddress.open (FileName, ios::app);
if (myAddress.is_open())
{
writeData();
}
break;
case 's':
case 'S'://Show record
myAddress.open (FileName, ios:: in);
if (myAddress.is_open())
{
readData();
}
break;
case 'e':
case 'E'://Exit
myAddress.close();
break;
default:
cout << "Invalid choice" << endl;
cout << endl << endl << endl;
break;
}
return;
}//end menu
void writeData(void) //Write the Address Info to a file
{
char answer = ' ';
char response = ' ';
if(myAddress.is_open())
{
//entering loop
while (answer != 'n' || answer != 'N')
{
cout << endl;
getline(cin, name);
cout << "\nEnter name: ";
getline(cin, name);
cout << "\nEnter Street: ";
getline(cin, street);
cout << "\nEnter City: ";
getline(cin, city);
cout << "\nEnter State: ";
getline(cin, state);
cout << "\nEnter Zip Code: ";
getline(cin, zipCode);
cout << endl;
outMyAddress << name << ", " << street << " ," << city << ", " << state << " ," << zipCode << endl;
record++;
cout << "\nWould you like to enter another record? (Y/N)" << endl;
cin >> response;
if (response == 'n' || response == 'N')
{
return menu();
}
}
}
myAddress.close();
}//end write data
void readData(void)
{
ifstream inMyAddress(FileName, ios::in);
if(myAddress.is_open())
{
string firstLine;
inMyAddress >> firstLine;
getline (myAddress, firstLine, '\n');
cout << endl;
cout << "Reading the file(s)..." << endl;
cout << endl;
//read data from a file
//use the split function to break a deliminated line of text into fields
cout << "Record #: " << record << endl;
string *theField = split(firstLine, ',');
cout << "Name......" << theField[0] << endl;
cout << "Street......" << theField[1] << endl;
cout << "City......" << theField[2] << endl;
cout << "State......" << theField[3] << endl;
cout << "Zip Code......" << theField[4] << endl;
}
inMyAddress.close();
system("pause");
return menu();
}//end read data
string * split(string theLine, char theDeliminator){
//Break theline into fields and save the fields to an array.
//Each field will occupy one element in a character array.
//theLine is a string with fields separated with theDeliminator character.
//Assumes the last field in the string is terminated with a newline.
//Useage: string *theFields = split(lineBuffer, ',');
//determine how many splits there will be so we can size our array
int splitCount = 0;
for(int i = 0; i < ((int)theLine.size()); i++){
if (theLine[i] == theDeliminator)
splitCount++;
return 0;
}
splitCount++; //add one more to the count because there is not an ending comma
//create an array to hold the fields
string* theFieldArray;
theFieldArray = new string[splitCount];
//split the string into seperate fields
string theField = "";
int commaCount = 0;
for(int i = 0; i < ((int)theLine.size()); i++){ //read each character and look for the deliminator
if (theLine[i] != theDeliminator) {
theField += theLine[i]; //build the field
}
else { //the deliminator was hit so save to the field to the array
theFieldArray[commaCount] = theField; //save the field to the array
theField = "";
commaCount++;
}
}
theFieldArray[commaCount] = theField; //the last field is not marked with a comma...
return theFieldArray;
} //end split
It tries to open a debugger when I try to read the file.