C++ Intermediate Level I/O Operation

Hi, I am glad to read helpful guides here in this forum and this time, I really need your help with regards to my problem.

Ok, so my C++ console application should be like a command prompt. There are "command lines" to execute in the command prompt.

Here are my main prompts for spotlight:

Encode - New data entry
View - Read data from text file
Search - Find record from text file
Update - Save changes per text line from the existing text file
Delete - Delete lines from the text file

Now, I manage to do the tasks for Encode, View, and Search..But unfortunately, not in Update..I've been working this for a week already.. See my code for more info.

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
#include "stdafx.h"
#include "iostream"
#include "string"
#include "fstream"

using namespace std;   //to omit the std

int _tmain(int argc, _TCHAR* argv[])
{
	string prompt;   //COMMAND PROMPT
	string yesno;	//YESNO CHOICE
start:  //start of the whole program
	
	//TITLE OF MY PROGRAM
	std::cout << "AMA COMPUTER LEARNING CENTER" << std::endl;
	cout << "  Fuentes Drive, Roxas City" << endl;
	cout << "===============================" << endl << endl;

begin:	//beginning the prompt
	cout << "@>";	//prompt
	getline(cin, prompt);	//INPUT PROMPT

//CLEAR
	if (prompt == "clear") {
		system("cls");
		goto begin;
	}
	if (prompt == "logoff") {
		system("cls");
		goto login;
	}

	//EMPTY
	else if (prompt == "") {
		goto begin;
	}

	//EXIT OR END
	else if (prompt == "exit" || prompt == "end") {
		return 0;
	}
//RESET OR RESTART
	else if (prompt == "reset" || prompt == "restart") {
		cout << "Do you want to reset the program? [Y/N] ";
		getline(cin, yesno);

		if (yesno == "Y" || yesno == "y") {
			system("cls");
			goto start;
		}
		else if (yesno == "N" || yesno == "n") {
			cout << endl;
			goto begin;
		}
	}

//ENCODE
	else if (prompt == "encode") {
		cout << "  ID number: ";
		getline(cin, idn);
		cout << " First name: ";
		getline(cin, fname);
		cout << "Middle name: ";
		getline(cin, mname);
		cout << "  Last name: ";
		getline(cin, lname);

	entry:
		cout << "Do you want to save your entry? [Y/N] ";
		getline(cin, yesno);

		if (yesno == "Y" || yesno == "y") {

			ofstream write("info.txt", ios::app);

			if (write.is_open()) {
				write << idn << "-" << lname << "-" << fname << "-" << mname << endl;
			}

			write.close();

			cout << "Record has been saved." << endl << endl;
			goto begin;
		}
		else if (yesno == "N" || yesno == "n") {
			cout << "Record has been discarded." << endl << endl;
			goto begin;
		}
		else {
			cout << "Invalid choice." << endl << endl;
			goto entry;
		}
	}
//READ DATA
	else if (prompt == "read" || prompt == "READ") {

		string idn[100], fname[100], mname[100], lname[100];   //for read records only
		int loop;

		ifstream read("info.txt");
		if (read.is_open()) {
			cout << "----------------------------" << endl;
			for (loop = 0; !read.eof(); loop++){
				getline(read, idn[loop], '-');
				getline(read, lname[loop], '-');
				getline(read, fname[loop], '-');
				getline(read, mname[loop], '\n');

				if (idn[loop] == "")
				{
					break;
				}
				else {
					cout << "    ID Number: " << idn[loop] << endl;
					cout << "    Last Name: " << lname[loop] << endl;
					cout << "   First Name: " << fname[loop] << endl;
					cout << "  Middle Name: " << mname[loop] << endl << "----------------------------" << endl << endl;
				}
			}
		}
		read.close();
		goto begin;
	}

//FOR SEARCH
	else if (prompt == "search" || prompt == "Search" || prompt == "SEARCH") {
		string idn[100], lname[100], fname[100], mname[100];
		string id, ln;
		int loop;
		cout << "Enter ID Number: ";
		getline(cin, id);
		cout << "Last name: ";
		getline(cin, ln);
		ifstream search("info.txt");
		if (search.is_open()) {
			for (loop = 0; !search.eof(); loop++) {
				if (search.good()) {
					getline(search, idn[loop], '-');
					getline(search, lname[loop], '-');
					getline(search, fname[loop], '-');
					getline(search, mname[loop], '\n');

					if (idn[loop].find(id) != string::npos && lname[loop].find(ln) != string::npos) {
						cout << "----------------------------" << endl;
						cout << "    ID Number: " << idn[loop] << endl;
						cout << "    Last Name: " << lname[loop] << endl;
						cout << "   First Name: " << fname[loop] << endl;
						cout << "  Middle Name: " << mname[loop] << endl << "----------------------------" << endl << endl;
						break;
					}

					else if (!search.eof())
					{
						continue;
					}

					else {
						cout << "Record not found." << endl;
					}
				} //IF
			} //FOR
		} //IF SEARCH CLOSE
		search.close();
		goto begin;
	}

//FOR DELETE
	else if (prompt == "del" || prompt == "DEL") {
		string idn[100], lname[100], fname[100], mname[100];
		string id;
		int loop;
		cout << "To continue, type the required IDN below" << endl << "ID Number : ";
		getline(cin, id);

		fstream del("info.txt");
		if (del.is_open()) {
			for (loop = 0; !del.eof(); loop++) {
				if (del.good()) {
					getline(del, idn[loop], '-');
					getline(del, lname[loop], '-');
					getline(del, fname[loop], '-');
					getline(del, mname[loop], '\n');
					if (idn[loop] == "") { break; }
					if (idn[loop].find(id) == string::npos) {
						cout << "----------------------------" << endl;
						cout << "    ID Number: " << loop << idn[loop] << endl;
						cout << "    Last Name: " << lname[loop] << endl;
						cout << "   First Name: " << fname[loop] << endl;
						cout << "  Middle Name: " << mname[loop] << endl << "----------------------------" << endl << endl;
						ofstream write("temp.txt", ios::app);

						if (write.is_open()) {
							write << idn[loop] << "-" << lname[loop] << "-" << fname[loop] << "-" << mname[loop] << endl;
							write.close();
						}
					}
				}
				else if (!del.eof())
				{
					loop--;
					cout << loop;
					continue;
				}
				else {
					cout << "Record not found." << endl;
				}
			} //IF
		} //FOR

		del.close();

		string idnR[100], fnameR[100], mnameR[100], lnameR[100];   //for read records only
		int x;

		ifstream read("temp.txt");
		if (read.is_open()) {
			for (x = 0; !read.eof(); x++){
				getline(read, idnR[x], '-');
				getline(read, lnameR[x], '-');
				getline(read, fnameR[x], '-');
				getline(read, mnameR[x], '\n');

				if (idn[x] == "")
				{
					break;
				}
				else {
					ofstream write("info.txt", ios::app);

					if (write.is_open()) {
						write << idn[x] << "-" << lname[x] << "-" << fname[x] << "-" << mname[x] << endl;
						write.close();
					}
				}
			}
		}
		read.close();
		goto begin;
	}


My question is, how can I update a line from a text file?

heres the format of my profile.txt

001-John-Doe
002-Steve-Murphy
003-Don-King

change
002-Steve-Murphy into 002-Steven-Murphy
Last edited on
I have no idea on what to do in that line. But here's my pseudo code.

1. Search the IDN.
2. Display the result to screen.
3. IF changes has been made, save the data, else discard changes.

Thanks
Topic archived. No new replies allowed.