Hmm C++ win32 console app is not working properly, any thoughts?

So I am writing a Win 32 console application that must do the following;

1. It should accept a series of names and addresses from the console.
2. 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.
3. Read the records from the text file, and display them in a user-friendly format.
4. Provide a menu to allow the user to append records to the file, display the records, or exit the application.

Also
it should accept an input like thus:

Append Records

Name..........John Smith
Street.........902 Union Ave
City............Any Town
State...........TX
Zip Code......78552

"Enter another Record? (Y/N)


File structure as thus;

John Smith, 902 Union Ave, Any Town, TX, 79552
Eric Jones, 345 State Way, Fresno, CA, 93432
...


And and output like the following

Show Records
__________________________________________
Record #1
Name...........John Smith
Street..........902 Union Ave
City.............Any Town
State...........TX
Zip Code......78552
__________________________________________
Record #2
Name...........Eric Jones
Street..........345 State Way
City.............Fresno
State...........CA
Zip Code.......93432



But the problem seems to be it is not accepting the name it just skips to the getlne adress and then when i go through the rest of the input it reads out
"File Error: Open Failed" instead of following the rest of the code.

Here is my code, any thoughts or advice would be much appreciated.

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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
#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.txt";

 

int main () {

menu();

return 0;

} //end main

 

void menu(void){

//allow user to choose to append records, display records or exit the program

enum menu: char{A, S, E};

char usersChoice = ' ';

 

cout << endl << "(A)ppend Records, (S)how Records, (E)xit\n";

cin >> usersChoice;

toupper(usersChoice);

 

switch(usersChoice){

case 'A':

writeData();

break;

case 'S':

readData();

break;

case 'E':

break;

}//end switch

}//end menu()

 

void writeData(void){

//Write the Address Info to a file

string name = " ";

string street = " ";

string city = " ";

string state = " ";

string zip = " ";

 

cout << endl << "Name...........";

getline(cin, name);

cout << endl << "Street.........";

getline(cin, street);

cout << endl << "City...........";

getline(cin, city);

cout << endl << "State..........";

getline(cin, state);

cout << endl << "Zip Code.......";

getline(cin, zip);

 

ofstream outMyStream(FileName, ios::app);

if (outMyStream.is_open()){

outMyStream << name << ',' << street << ','

<< city << ',' << state << ',' << zip;

outMyStream.close();

}

else{

cout << "File Error: Open Failed";

}//end if

menu();

}//end write data

 

void readData(void){

//read data from a file

string lineBuffer = " ";

int count = 0;

 

ifstream inMyStream(FileName);

if (inMyStream.is_open()){

while (!inMyStream.eof()){

count ++;

getline(inMyStream, lineBuffer);

//use the split function to break a

//deliminated line of text into fields

string * theFieldArray[5] = {split(lineBuffer, ',')};

cout << endl << "Record #" << count;

cout << endl << "Name..........." << theFieldArray[0] <<

endl << "Street........." << theFieldArray[1] <<

endl << "City..........." << theFieldArray[2] <<

endl << "State.........." << theFieldArray[3] <<

endl << "Zip Code......." << theFieldArray[4] << endl;

}//end while

}//end if

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 < static_cast<int>(theLine.size()); i++){

if (theLine[i] == theDeliminator)

splitCount++;

}

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 < static_cast<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 
Couple of things for now.
Line 51: toupper(usersChoice); returns an uppercase character, but doesn't change input. You need to change it to: usersChoice = toupper(usersChoice); or use it in the switch statement: switch (toupper(usersChoice))

The name is skipped because when you read the menu choice, the line feed is still in the buffer, so getline reads an empty line. You can, for example, use cin.ignore() before reading data in.

I really don't like the recursive calls to menu(). Every time a user decides to do anything, the program will increase the call stack. While this will work in this case, it's not a good idea in my opinion. The program should state its intent through code. You don't want to recursively call menu, you want to show menu until the user wants to quit (the part in bold is a hint on how to modify your menu function).

Also, what happens if user inputs an incorrect menu item choice? (Hint: default:).
Okay so i edited my code to do the following but im having a problem with it not being able to read or write to the file? Im confused...


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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
#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.txt";
const char delimiter = ',';

 

int main () {

menu();

return 0;

} //end main

 

void menu(void){

//allow user to choose to append records, display records or exit the program

enum menu: char{A, S, E};

char usersChoice = ' ';

 

cout << endl << "(A)ppend Records, (S)how Records, (E)xit\n";

cin >> usersChoice;
cin.ignore();
cout << endl;
usersChoice = toupper(usersChoice);

 

switch(usersChoice){

case 'A':

writeData();

break;

case 'S':

readData();

break;

case 'E':

break;

}//end switch

}//end menu()

 

void writeData(void){

//Write the Address Info to a file

string name = " ";

string street = " ";

string city = " ";

string state = " ";

string zip = " ";

 

cout << endl << "Name...........";

getline(cin, name);

cout << endl << "Street.........";

getline(cin, street);

cout << endl << "City...........";

getline(cin, city);

cout << endl << "State..........";

getline(cin, state);

cout << endl << "Zip Code.......";

getline(cin, zip);

 

ofstream outMyStream(FileName, ios::app);

if (outMyStream.is_open()){

outMyStream << name << ',' << street << ','

<< city << ',' << state << ',' << zip;

outMyStream.close();

}

else{

cout << "File Error: Open Failed";

}//end if

menu();

}//end write data

 

void readData(void){

//read data from a file

string lineBuffer = " ";

int count = 0;

 

ifstream inMyStream(FileName);

if (inMyStream.is_open()){

while (!inMyStream.eof()){

count ++;

getline(inMyStream, lineBuffer);

//use the split function to break a

//deliminated line of text into fields

string * theFieldArray[5] = {split(lineBuffer, ',')};

cout << endl << "Record #" << count;

cout << endl << "Name..........." << theFieldArray[0] <<

endl << "Street........." << theFieldArray[1] <<

endl << "City..........." << theFieldArray[2] <<

endl << "State.........." << theFieldArray[3] <<

endl << "Zip Code......." << theFieldArray[4] << endl;

}//end while

}//end if

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 < static_cast<int>(theLine.size()); i++){

if (theLine[i] == theDeliminator)

splitCount++;

}

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 < static_cast<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 
Topic archived. No new replies allowed.