sequential files

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?


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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#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.
string *theField = split(firstLine, ',');

split() returns a string*, but in it you are always returning 0 (NULL) so whenever you deference theField, you get an error.
Topic archived. No new replies allowed.