find and replace employee information in text file

closed account (L092y60M)
I am completely lost on how to search for a word in a text file and replace it with a new one. Such as employee address, or first name, etc.

Here's my 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
#include <iostream>
#include <string>
#include <fstream> 

using namespace std;

int main(){
	ifstream inFile; //declare ifstream as inFile and ofstream as outFile
	ofstream outFile;

	string firstName, lastName, employeeAdd, salary, employeeID, employeeSearch, employeeModify;
	string newFirst, newLast, newAdd, newSalary, newEmployID;
	int choice;
	char newEmployeeChoice;
	

	outFile.open("employee.txt", ios::app); //open employee.txt and append new information

	cout << "Please enter 1: To enter employee information: " << endl; //ask user for what they want to choose for the employee
	cout << "Please enter 2: To look for employee information: " << endl;
	cout << "Please enter 3: To modify employee information: " << endl;
	cin >> choice;

	if (choice == 1){ //if choice is 1 user asks employee for information
		do{
			cout << "Please enter your employee ID: "; //user input employee id
			cin >> employeeID;

			cout << "Please enter your first name please: "; //user inputs first name
			cin.clear();
			cin.sync();
			getline(cin, firstName);

			cout << "Please enter your last name: "; //user inputs last name
			cin.clear();
			cin.sync();
			getline(cin, lastName);

			cout << "Please enter your home address: "; //user inputs home address
			cin.clear();
			cin.sync();
			getline(cin, employeeAdd);

			cout << "Please enter in your monthly salary: "; //user inputs monthly salary
			cin >> salary;

			outFile << employeeID << "," << firstName << "," << lastName << "," << employeeAdd << "," << salary << endl; //output user info in employee.txt

			cout << "\nWould you like to enter in another employee: Y for yes: "; //ask user if they have another entry
			cin >> newEmployeeChoice;
		} while (newEmployeeChoice == 'Y' || newEmployeeChoice == 'y'); //while loop if user inputs yes they can enter in another employee
	}
	else if (choice == 2){ //if choice is 2 the user will have to provide employee ID to bring up information
		outFile.close(); //close employee.txt as outFile
		inFile.open("employee.txt"); //open employee.txt as inFile

		cout << "Please enter in the employee ID of the employee you want to search: "; //ask user for employee ID
		cin >> employeeSearch;

		while (getline(inFile, employeeID, ','), //while loop so it can recieve all the information from text file
			getline(inFile, firstName, ','),
			getline(inFile, lastName, ','),
			getline(inFile, employeeAdd, ','),
			getline(inFile, salary, '\n'))

		if (employeeSearch == employeeID){ //if statement will compare employee ID to the one in employees.txt and the one user entere, if matches then info is displayed
			cout << "\nEmployee ID: " << employeeID << endl;
			cout << "First Name: " << firstName << endl;
			cout << "Last Name: " << lastName << endl;
			cout << "Employee Address: " << employeeAdd << endl;
			cout << "Employee Salary: $" << salary << endl;
		}
		inFile.close();
	}
	else if (choice == 3){
		inFile.open("employee.txt");

		cout << "Please enter the employee number of the employee you want to modify: ";
		cin >> employeeSearch;

		while (getline(inFile, employeeID, ','), //while loop so it can recieve all the information from text file
			getline(inFile, firstName, ','),
			getline(inFile, lastName, ','),
			getline(inFile, employeeAdd, ','),
			getline(inFile, salary, '\n'))

		if (employeeSearch == employeeID){ //if statement will compare employee ID to the one in employees.txt and the one user entere, if matches then info is displayed
			cout << "\nEmployee ID: " << employeeID << endl;
			cout << "First Name: " << firstName << endl;
			cout << "Last Name: " << lastName << endl;
			cout << "Employee Address: " << employeeAdd << endl;
			cout << "Employee Salary: $" << salary << endl << endl;
		}

		cout << "Which item would you like to modify: " << endl;
		cout << "Enter F: for First Name." << endl;
		cout << "Enter L: for Last Name." << endl;
		cout << "Enter A: for Address." << endl;
		cout << "Enter S: for Salary." << endl;
		cin >> employeeModify;



		if (employeeModify == "F")
		{

			cout << "What would you like to replace the first name to: ";
			cin >> newFirst;

			//don't know what to do next
		}

	system("pause");
	return 0;

}


It is under choice 3, most of my program runs correctly but I am just stuck at this part.

Thank you.
Last edited on
What's the file formatting? Can you give us an example?
Unless you are working with a fixed size data set.. you will need to create a temporary file and copy the lines up to the point where you wish to make changes, output the modified line, and then the remaining file.

Delete the old, and replace it with the new.
After getting all the employee info, let the user change what he wants. So you'll have the all the data, including the changed one. Then you truncate the file and write all the data again.
Topic archived. No new replies allowed.