-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBlea_Database.cpp
More file actions
152 lines (137 loc) · 5.12 KB
/
Blea_Database.cpp
File metadata and controls
152 lines (137 loc) · 5.12 KB
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
#include <iostream>
#include <fstream>
#include <string>
#include <limits> // Required for numeric_limits
using namespace std;
fstream file;
string filename = "data.txt";
struct Person {
string id, firstName, lastName;
int income;
};
Person* search(string id) {
Person *p2 = new Person;
char empId[6], firstName[16], lastName[16], incomeStr[11]; // Increased sizes by 1 for null terminator
file.clear();
file.seekg(0, ios::beg); // Rewind to the beginning for each search
while (file.read(empId, 5)) {
empId[5] = '\0';
p2->id = empId;
if (p2->id == id) {
// Reading rest of data
file.read(firstName, 15);
firstName[15] = '\0'; // Null-terminate the string
file.read(lastName, 15);
lastName[15] = '\0'; // Null-terminate the string
file.read(incomeStr, 10);
incomeStr[10] = '\0'; // Null-terminate the string
// Save to structure Person
p2->firstName = firstName;
p2->lastName = lastName;
p2->income = stoi(incomeStr); // Use stoi for integer conversion
return p2;
}
file.seekg(40, ios::cur);
}
delete p2; // Deallocate if not found
return nullptr; // Return nullptr if not found
}
void get_employee() {
string empId;
cout << "Enter Employee id: ";
cin >> empId;
Person* employee = search(empId);
if (employee == nullptr) {
cout << "Employee not found." << endl;
} else {
cout << "Employee ID: " << employee->id << endl;
cout << "First Name: " << employee->firstName << endl;
cout << "Last Name: " << employee->lastName << endl;
cout << "Income: " << employee->income << endl; // Fixed typo here
delete employee;
}
}
void list() {
char id[6], firstName[16], lastName[16], incomeStr[11]; // Increased sizes by 1
file.clear();
file.seekg(0, ios::beg);
while (file.read(id, 5)) {
id[5] = '\0'; // Null-terminate the string
file.read(firstName, 15);
firstName[15] = '\0'; // Null-terminate the string
file.read(lastName, 15);
lastName[15] = '\0'; // Null-terminate the string
file.read(incomeStr, 10);
incomeStr[10] = '\0'; // Null-terminate the string
// No need to convert to float here since income is an int in the struct
cout << id << " " << firstName << " " << lastName << " " << incomeStr << endl;
}
}
void write_data(Person p) {
file.clear();
file.write(p.id.c_str(), 5);
file.write(p.firstName.c_str(), 15);
file.write(p.lastName.c_str(), 15);
file << p.income << endl; // Use endl for newline and implicit conversion to string
file.flush();
}
void new_employee() {
string empId;
while (1) {
cout << "\n*** New Employee ***\n";
cout << "Enter employee id (-1 to end): ";
cin >> empId;
if (empId == "-1") break; // stop when user enters -1
// search if the employee exist, call to search function
// Remember search function return a pointer to struct ‘Person’ if the employee
Person* employee = search(empId);
// otherwise, it returns null pointer (i.e. zero pointer)
// If the employee id exist, then exit from the function by calling return.
if (employee != nullptr) {
cout << "Employee ID already exists." << endl;
return;
}
// If employee id does not exist, then continue ask the user about the employee
// information: first name, last name, and income.
// complete code
// Save data to Person struct
Person p;
p.id = empId;
cout << "Enter first name: ";
cin >> p.firstName;
cout << "Enter last name: ";
cin >> p.lastName;
cout << "Enter income: ";
cin >> p.income;
cout << "Saving data: ";
write_data(p);
}
}
int main() {
file.open(filename, ios::in | ios::out); // open file for read and write
if (!file) {
file.open(filename, ios::out); // create the file
file.close();
file.open(filename, ios::in | ios::out);
}
// Menu:
int option = -1;
while (1) {
cout << "*** Menu ***" << endl;
cout << "1) New employee\n";
cout << "2) Search employee information\n";
cout << "3) List employee\n";
cout << "option: ";
cin >> option;
switch (option) {
case 1: new_employee(); break;
case 2: get_employee(); break;
case 3: list(); break;
default: // any other number will exit the menu loop
return 0;
}
}
// Close files
file.close();
return 0;
}