delete record

Pages: 12
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
#include "employee.h"

void readRecord(Employee *theEmployee , int &size){

	ifstream inFile;
	
	inFile.open( "employee.txt" );

	if( inFile.is_open() ){
		inFile >> size;
		inFile.ignore(1, ':');
		cin.get();
		for (int i = 0 ; i < size ; ++i)
		{
			getline( inFile , theEmployee[i].EmployeeID );
			getline( inFile , theEmployee[i].EmployeeName );
			getline( inFile , theEmployee[i].EmployeeDepartment );
			getline( inFile , theEmployee[i].EmployeeDOB );
			getline( inFile , theEmployee[i].EmployeeAddress );
			inFile >> theEmployee[i].EmployeeWorkHours;
			inFile >> theEmployee[i].PayRate;
			inFile >> theEmployee[i].totalSalary;
			inFile.ignore(1,':');
		}
	}
}

void addRecord(Employee *theEmployee , int &size){

		ofstream outFile;

		cout<<"Size: "<< size;
		cout << "\nEnter Employee ID [Mix of Char & Integer] : ";
		cin.ignore();
		getline( cin,theEmployee[size].EmployeeID );
		
		cout << "Enter Name : ";
		getline( cin,theEmployee[size].EmployeeName );

		cout << "Enter Employee Department : ";
		getline( cin,theEmployee[size].EmployeeDepartment );

		cout << "Date of birthday [dd/mm/yy] : ";
		//cin.ignore();
		getline( cin,theEmployee[size].EmployeeDOB );

		cout << "Enter Address : ";
		getline( cin,theEmployee[size].EmployeeAddress );
	
		do{
			cout << "Enter Work Hour : ";
			cin  >> theEmployee[size].EmployeeWorkHours;
			if( theEmployee[size].EmployeeWorkHours < 1 ){
				cout << "\nInvalid Input for Work Hours !" << endl;
			}
		}while( theEmployee[size].EmployeeWorkHours < 1 );

		do{
			cout << "Enter Pay Rate : ";
			cin  >> theEmployee[size].PayRate;
			if( theEmployee[size].PayRate < 1 )
				cout << "\nInvalid Input for Pay Rate !" << endl;
		}while( theEmployee[size].PayRate < 1 );
	
		theEmployee[size].totalSalary = theEmployee[size].EmployeeWorkHours * theEmployee[size].PayRate ;
	 
		outFile.open( "employee.txt" , ios:: out  );
		size++;
		outFile << size << "\n" ;

		for( int i = 0 ; i < size ; i++ ){
			outFile	<< theEmployee[i].EmployeeID          << endl << theEmployee[i].EmployeeName << endl
					<< theEmployee[i].EmployeeDepartment  << endl
					<< theEmployee[i].EmployeeDOB << endl << theEmployee[i].EmployeeAddress
					<< endl << theEmployee[i].EmployeeWorkHours << endl << theEmployee[i].PayRate    
					<< endl << theEmployee[i].totalSalary << endl;
		}

		outFile.close();
}

void displaySpecificEmployee(Employee *theEmployee , int &size){
	
	string SpecificEmployeeID = "";
	int foundIndex    = 0;
	bool found        = 0;

	system( "cls" );
	cin.ignore();
	cout << "\tSearch a particular employee's payroll "               << endl
		 << "-------------------------------------------------------" << endl << endl
		 << "Enter Employee ID : ";

	
	getline(cin,SpecificEmployeeID);

	for( int i = 0 ; i < size ; i++ ){
		if(  theEmployee[i].EmployeeID==SpecificEmployeeID ){
			found = true;
			foundIndex = i;
		}
	}

	if( found == true ){
		cout << "\nEmployee ID   : "           << theEmployee[foundIndex].EmployeeID          << endl
			 << "Employee Name : "             << theEmployee[foundIndex].EmployeeName		  << endl
		     << "Employee Department    : "    << theEmployee[foundIndex].EmployeeDepartment  << endl
			 << "Employee Date Of Birth : "    << theEmployee[foundIndex].EmployeeDOB		  << endl
			 << "Employee Address    : "       << theEmployee[foundIndex].EmployeeAddress	  << endl
		     << "Employee Work Hours : "       << theEmployee[foundIndex].EmployeeWorkHours   << endl
			 << "Employee Pay Rate   : "       << theEmployee[foundIndex].PayRate			  << endl
			 << "Employee Salary     : "       << theEmployee[foundIndex].totalSalary         << endl;
	}
	else
		cout << "No Any Record found ! " << endl;

}

void displayParticularDepartment(Employee *theEmployee , int &size){

	bool match = 0;
	int foundIndex = 0;
	string theDepartment;

	cin.ignore();
	cout << "Enter Department : ";
	getline( cin , theDepartment );

	cout << "Department : " << theDepartment << endl << endl;

	for( int i = 0 ; i < size ; i++ ){
		if( theEmployee[i].EmployeeDepartment == theDepartment ){
			match = true;
			foundIndex = i;
		}
		if( match == true ){
			cout << "\nRecord " << ( i + 1 ) << "\n-----------------------";
			cout << "\nEmployee ID   : "           << theEmployee[foundIndex].EmployeeID          << endl
				 << "Employee Name : "             << theEmployee[foundIndex].EmployeeName		  << endl
				 << "Employee Department    : "    << theEmployee[foundIndex].EmployeeDepartment  << endl
				 << "Employee Date Of Birth : "    << theEmployee[foundIndex].EmployeeDOB		  << endl
				 << "Employee Address    : "       << theEmployee[foundIndex].EmployeeAddress	  << endl
				 << "Employee Work Hours : "       << theEmployee[foundIndex].EmployeeWorkHours   << endl
				 << "Employee Pay Rate   : "       << theEmployee[foundIndex].PayRate			  << endl
				 << "Employee Salary     : "       << theEmployee[foundIndex].totalSalary         << endl ;
		}
	}
}

void ModifySpecificEmployee(Employee *theEmployee , int &size){

	string SpecificEmployeeID = "";
	bool found = 0;
	int foundIndex = 0;

	cin.ignore();
	cout << "\tModify Employee\n---------------------------------" << endl << endl
		 << "Enter Employee ID : "               ;
	getline( cin , SpecificEmployeeID );

	for( int i = 0 ; i < size ; i++ ){
		if(  theEmployee[i].EmployeeID == SpecificEmployeeID ){
			found = true;
			foundIndex = i;
		}
	}

	if( found == true ){
		cout << "\n\tRecord :\n---------------------" << endl
			 << "Employee ID   : "             << theEmployee[foundIndex].EmployeeID           << endl
			 << "Employee Name : "             << theEmployee[foundIndex].EmployeeName		   << endl
		     << "Employee Department    : "    << theEmployee[foundIndex].EmployeeDepartment   << endl
			 << "Employee Date Of Birth : "    << theEmployee[foundIndex].EmployeeDOB		   << endl
			 << "Employee Address    : "       << theEmployee[foundIndex].EmployeeAddress	   << endl
		     << "Employee Work Hours : "       << theEmployee[foundIndex].EmployeeWorkHours    << endl
			 << "Employee Pay Rate   : "       << theEmployee[foundIndex].PayRate			   << endl
			 << "Employee Salary     : "       << theEmployee[foundIndex].totalSalary          << endl << endl
			 << "1. Change EmployeeID"    << endl
			 << "2. Change EmployeeName " << endl
			 << "3. Change Department "   << endl
			 << "4. Change Date Of Birth" << endl
			 << "5. Change Address "      << endl
			 << "6. Change Work Hours "   << endl
			 << "7. Change Pay Rate "     << endl;
	}
}

void deleteSpecificEmployeeRecord(Employee *theEmployee , int &size){

	int choice = 0;
	string SpecificEmployeeID = "";
	bool found = 0;
	int foundIndex = 0;

	cin.ignore();
	cout << "\tDelete Employee Record\n---------------------------------" << endl << endl
		 << "Enter Employee ID : "               ;
	getline( cin , SpecificEmployeeID );

	for( int i = 0 ; i < size ; i++ ){
		if(  theEmployee[i].EmployeeID == SpecificEmployeeID ){
			found = true;
			foundIndex = i;
		}
	}

	if( found == true ){
		cout << "\n\tRecord :\n---------------------" << endl
			 << "Employee ID   : "             << theEmployee[foundIndex].EmployeeID           << endl
			 << "Employee Name : "             << theEmployee[foundIndex].EmployeeName		   << endl
		     << "Employee Department    : "    << theEmployee[foundIndex].EmployeeDepartment   << endl
			 << "Employee Date Of Birth : "    << theEmployee[foundIndex].EmployeeDOB		   << endl
			 << "Employee Address    : "       << theEmployee[foundIndex].EmployeeAddress	   << endl
		     << "Employee Work Hours : "       << theEmployee[foundIndex].EmployeeWorkHours    << endl
			 << "Employee Pay Rate   : "       << theEmployee[foundIndex].PayRate			   << endl
			 << "Employee Salary     : "       << theEmployee[foundIndex].totalSalary          << endl << endl;
	}
	
	cout << "1. Delete Record " << endl
		 << "2. Cancel "        << endl;

	cin  >> choice;

	if( choice == 1){

	}
	else if( choice == 2 ){

	}
}
can i ask how to delete the record ? not really sure how to do it .

inside my txt file data will be
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
2
ad125
chirsjye
sales
13-10-1991
25 , mk 10
50
20
1000
ad126
wendy
sales
1-10-1991
25 , mk 13
100
20
2000


so when i input employee ID
Enter ID : ad126

the output detail will be show and
option will be choose
1
2
1. Delete
2. Cancel


if i choose "delete"
the record will be deleted and update the txt file as well:
1
2
3
4
5
6
7
8
9
1
ad125
chirsjye
sales
13-10-1991
25 , mk 10
50
20
1000


the 2 will become 1 , i totally lost the algo .
Here is an example how to delete the record:
1
2
3
4
5
6
7
8
void deleteRecord(Employee *theEmployee , int index /*or let the user enter the index */, int &size){
...

--size; // size must not be 0
theEmployee[index] = theEmployee[size]; // if the order doesn't matter

// see line 71
...
Last edited on
You should rewrite the whole file with the new data.
Last edited on
@coder 777
erm . int index? but i think i pass it though size is enough d rite?
can just provide a sentences coding to me?
then i can get it d
i mean how to delete the certain record and remove . sorry


@vlad . i know i should rewrite about it . the problem i don't know how to delete the certain person and remove all the details on it .


You need an index that tells which record should be removed.

Line 4 and 5 of my example will remove the record from your array/table. Then you need to write it back to the file like you did in addRecord() (line 67, 69, 71 and 79)

alternative:
1
2
3
4
5
6
7
8
9
10
11
12
13
void deleteRecord(Employee *theEmployee , int &size){
...

int index;
		cout << "Enter the index of the record to remove: ";
		cin >> index;

--size; // This decreases the size (must be greater then 0)
theEmployee[index] = theEmployee[size]; // This removes the entry / replace with last entry (if the order doesn't matter)

// see addRecord on how to write the data back
...
}


if you want that the user may enter the name or something, well, the you need search the appropriate entry.
@coder777

thanks for your lesson . i learn the new things again . inside my code . i forget to add a
--size;

i forget have to re-write my size . keep wonder why my size cannot decrease . once time . thanks again .


by the way , if i wan modify specific employee the certain information ?

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
void ModifySpecificEmployee(Employee *theEmployee , int &size){

	string SpecificEmployeeID = "";
	bool found = 0;
	int foundIndex = 0;

	cin.ignore();
	cout << "\tModify Employee\n---------------------------------" << endl << endl
		 << "Enter Employee ID : "               ;
	getline( cin , SpecificEmployeeID );

	for( int i = 0 ; i < size ; i++ ){
		if(  theEmployee[i].EmployeeID == SpecificEmployeeID ){
			found = true;
			foundIndex = i;
		}
	}

	if( found == true ){
		cout << "\n\tRecord :\n---------------------" << endl
			 << "Employee ID   : "             << theEmployee[foundIndex].EmployeeID           << endl
			 << "Employee Name : "             << theEmployee[foundIndex].EmployeeName		   << endl
		     << "Employee Department    : "    << theEmployee[foundIndex].EmployeeDepartment   << endl
			 << "Employee Date Of Birth : "    << theEmployee[foundIndex].EmployeeDOB		   << endl
			 << "Employee Address    : "       << theEmployee[foundIndex].EmployeeAddress	   << endl
		     << "Employee Work Hours : "       << theEmployee[foundIndex].EmployeeWorkHours    << endl
			 << "Employee Pay Rate   : "       << theEmployee[foundIndex].PayRate			   << endl
			 << "Employee Salary     : "       << theEmployee[foundIndex].totalSalary          << endl << endl
			 << "1. Change EmployeeID"    << endl
			 << "2. Change EmployeeName " << endl
			 << "3. Change Department "   << endl
			 << "4. Change Date Of Birth" << endl
			 << "5. Change Address "      << endl
			 << "6. Change Work Hours "   << endl
			 << "7. Change Pay Rate "     << endl;
	}
}



if i change the Name .

for this i know it need to
//addRecord to write the file back

but here i not really know how to modify certain information
can give some advise on it? thanks.

by the way , if i wan modify specific employee the certain information ?

you can do so:
1
2
3
4
5
6
7
8
9
switch(choice)
{
...
	case 2:
		cout << "Enter Name : ";
		getline( cin,theEmployee[foundIndex].EmployeeName );
		break;
...
}


for this i know it need to
//addRecord to write the file back
No, instead put the part you need in it's own function:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void writeRecord(Employee *theEmployee , int &size){

		ofstream outFile;

		outFile.open( "employee.txt" , ios:: out  );
		outFile << size << "\n" ;

		for( int i = 0 ; i < size ; i++ ){
			outFile	<< theEmployee[i].EmployeeID          << endl << theEmployee[i].EmployeeName << endl
					<< theEmployee[i].EmployeeDepartment  << endl
					<< theEmployee[i].EmployeeDOB << endl << theEmployee[i].EmployeeAddress
					<< endl << theEmployee[i].EmployeeWorkHours << endl << theEmployee[i].PayRate    
					<< endl << theEmployee[i].totalSalary << endl;
		}

		outFile.close();
}
Which means you modify the data and then call writeRecord().

Your addRecord() would look like this:
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
void addRecord(Employee *theEmployee , int &size){

		cout<<"Size: "<< size;
		cout << "\nEnter Employee ID [Mix of Char & Integer] : ";
		cin.ignore();
		getline( cin,theEmployee[size].EmployeeID );
		
		cout << "Enter Name : ";
		getline( cin,theEmployee[size].EmployeeName );

		cout << "Enter Employee Department : ";
		getline( cin,theEmployee[size].EmployeeDepartment );

		cout << "Date of birthday [dd/mm/yy] : ";
		//cin.ignore();
		getline( cin,theEmployee[size].EmployeeDOB );

		cout << "Enter Address : ";
		getline( cin,theEmployee[size].EmployeeAddress );
	
		do{
			cout << "Enter Work Hour : ";
			cin  >> theEmployee[size].EmployeeWorkHours;
			if( theEmployee[size].EmployeeWorkHours < 1 ){
				cout << "\nInvalid Input for Work Hours !" << endl;
			}
		}while( theEmployee[size].EmployeeWorkHours < 1 );

		do{
			cout << "Enter Pay Rate : ";
			cin  >> theEmployee[size].PayRate;
			if( theEmployee[size].PayRate < 1 )
				cout << "\nInvalid Input for Pay Rate !" << endl;
		}while( theEmployee[size].PayRate < 1 );
	
		theEmployee[size].totalSalary = theEmployee[size].EmployeeWorkHours * theEmployee[size].PayRate ;
	 
		size++;
		writeRecord(theEmployee, size);
}


You have a lot of repeating code (like the search for an employee or showing the data) put that in it's own function and just call it. Everything will be nicer then
@code . thanks for ur patiently for teaching me . i done it already .
Last edited on
Beside like this

for my this function
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void writeRecord(Employee *theEmployee , int &size){

		ofstream outFile;

		outFile.open( "employee.txt" , ios:: out  );
		outFile << size << "\n" ;

		for( int i = 0 ; i < size ; i++ ){
			outFile	<< theEmployee[i].EmployeeID          << endl << theEmployee[i].EmployeeName << endl
					<< theEmployee[i].EmployeeDepartment  << endl
					<< theEmployee[i].EmployeeDOB << endl << theEmployee[i].EmployeeAddress
					<< endl << theEmployee[i].EmployeeWorkHours << endl << theEmployee[i].PayRate    
					<< endl << theEmployee[i].totalSalary << endl;
		}

		outFile.close();
}


the file won't be save as a txt but just keep it as a memory?
as my question required . i have to save the file in binary

i search a lot that information they say need to save as const char*

but i really no idea on the cast things . how should it to be save in binary and random access file? for my program..

Last edited on
the flag ios::binary affects the line endings only. Windows has two markers as line end: "\r\n". With the flag ios::binary it's not converted to that format. But maybe that's acceptable (only a few text editor like Notepad cannot cope with this format).


the file is supposed to appear. Maybe it just doesn't appear where you expect it? You may provide a full path. Furthermore you should check the condition of the stream (like is_open() or good())


Yesterday I read something about id. Is that solved?
ya . the ID
i just use a for loop to check that whether it's same or not. if same will ask for user to re-enter.

okay i get it.

but for my question . it required me to save the file in binary
The program should store the employee’s records in a random access file and supports a variety of operations such as add a new employee record, modify an employee record, search and display employee records, etc.

and for my own purpose . how to convert a string to unique ID?
The employee id should be unique.
A uniue ID isn't something particular. It's ok to handle it like you do.

if you want random access to your file you need to do something.
The easiest way is probably providing a fixed length for your record (which implies that you have to limit the string length entered by the user).

if you have a record of a certain length you can seek to this record by multiplying the record length with the index in question
okay, erm, mind provid an example?
well, random a access is somewhat advanced...
the point is that reading/writing all the data is very slow. with random access you have just one record at any time which you manipulate and write back.

reading would look like this
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
const int RecordSize = ...

void readRecord(Employee *theEmployee , int index){ // Note that you have an index instead of size

	ifstream inFile;
	
	inFile.open( "employee.txt" );

	if( inFile.is_open() ){
			if(inFile.seekg(index * RecordSize)){ // index * RecordSize is the offset of your record
					getline( inFile , theEmployee->EmployeeID ); // Note that you do not have an array anymore
					getline( inFile , theEmployee->EmployeeName );
					getline( inFile , theEmployee->EmployeeDepartment );
					getline( inFile , theEmployee->EmployeeDOB );
					getline( inFile , theEmployee->EmployeeAddress );
					inFile >> theEmployee->EmployeeWorkHours;
					inFile >> theEmployee->PayRate;
					inFile >> theEmployee->totalSalary;
			}
			inFile.close();
		}
	}
}
bt if like that , i cant read my size already? beside that , i only cn hold 1 record? other record wil be discard as well?
why i nid to deClare the constant size? So just declare it to 1 ? Since index multiply the constant value ,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
void readRecord(Employee *theEmployee , int &index){ // Note that you have an index instead of size

	ifstream inFile;
	string filename;

	cout << "Enter filename to be read : ";
	getline( cin, filename );
	inFile.open( filename.c_str() , ios :: binary );

	if( inFile.is_open() ){
			if(inFile.seekg(index * RecordSize)){ // index * RecordSize is the offset of your record
					getline( inFile , theEmployee->EmployeeID ); // Note that you do not have an array anymore
					getline( inFile , theEmployee->EmployeeFirstName );
					getline( inFile , theEmployee->EmployeeLastName );
					getline( inFile , theEmployee->EmployeeDepartment );
					getline( inFile , theEmployee->EmployeePosition );
					inFile >> theEmployee->EmployeeWorkHours;
					inFile >> theEmployee->PayRate;
					inFile >> theEmployee->totalSalary;
			}
			inFile.close();
	}
}


err . but i can't get any detail on my txt . it read nothing . why d?
The problem is this: random access file

random access means that you can access a series of data anywhere. Take a look at this:

http://en.wikipedia.org/wiki/Random_access


an array fulfills this requirement. So unfortunately since it says file you have to map the behaviour of an array to a file.

i cant read my size already?
You don't need it because the file size implies the number of records. Like so:

number_of_records = file_size / record_size

Having the size in your file might be a good idea regarding deleting a record (because shrinking a file might be a problem)


beside that , i only cn hold 1 record? other record wil be discard as well?
Why do you think so? in addRecord() you add the record to the file. All records are stored in the file, but in memory you need just the record in question.

why i nid to deClare the constant size? So just declare it to 1 ? Since index multiply the constant value ,
Say what? The constant RecordSize (or however you like to named it) must be large enough to hold all the data of your record.

I recommend that you put your current working code aside, so that you have a reference.
What you need to understand is that for random access file you must know the postion of the record in question within the file (using seek). A record with a fixed size is only one approach. Maybe you can find a better way for you?
isn't u mean that i can declare any size for my const RecordSize?

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
const RecordSize = 100;

void readRecord(Employee *theEmployee , int index){ // Note that you have an index instead of size

	ifstream inFile;
	string filename;

	cout << "Enter filename to be read : ";
	getline( cin, filename );
	inFile.open( filename.c_str() , ios :: binary );

	if( inFile.is_open() ){
			if(inFile.seekg(index * RecordSize)){ // index * RecordSize is the offset of your record
					getline( inFile , theEmployee->EmployeeID ); // Note that you do not have an array anymore
					getline( inFile , theEmployee->EmployeeFirstName );
					getline( inFile , theEmployee->EmployeeLastName );
					getline( inFile , theEmployee->EmployeeDepartment );
					getline( inFile , theEmployee->EmployeePosition );
					inFile >> theEmployee->EmployeeWorkHours;
					inFile >> theEmployee->PayRate;
					inFile >> theEmployee->totalSalary;
			}
			inFile.close();
	}
}


but when me pass value to the index

the value holding is 0 .

my main function()

1
2
3
4
5
6
7
8
int main(){
	
	Employee theEmployee[30];
	int size = 0;
	char choice;
	char choice2;

	readRecord( theEmployee , size );


just a part of the code .

so am i correct until now? i try to read what is random access as my lecturer teach me , but she not showing any example so i hard to get understand , she said i can access any data from any line .
Pages: 12