Search functions not returning proper values

Part of my assignment requires making a search function to find employees in a database. I got the function that searches by id (a five character string of digits) to work, but I can't seem to get the one that goes by last name to work. It returns no results, and in a delete function used later, deletes chunks at random without reference to what is put in. Could anyone shed some light on what's going wrong?

The problem function:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void nameSearch(Employee db[],int nEmployees){
     char choice[13];
     cin.get();
     cout << "Enter search value: ";
     cin.getline(choice,13);
     cout << "No. EmpID Employee Name        Email                Salary" << endl;
     cout << "--- ----- -------------------- -------------------- ----------" << endl;
     for (int count = 0; count<nEmployees; count++){
         if (strcmp(db[count].lastName,choice)==0) {
            cout.setf(ios::fixed | ios::showpoint);
            cout.precision(2);
            cout << setw(3) << count + 1 << " " << setw(5) << db[count].id << " " << setw(10) << db[count].firstName << " " << db[count].lastName
                 << setw(24) << db[count].email << " " << setw(10) << db[count].salary << endl;
         }
}
return;
}



The function that searches by id, which works:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void idSearch(Employee db[],int nEmployees){
     char choice[6];
     cin.get();
     cout << "Enter search value: ";
     cin.getline(choice,6);
     cout << "No. EmpID Employee Name        Email                Salary" << endl;
     cout << "--- ----- -------------------- -------------------- ----------" << endl;
     for (int count = 0; count<nEmployees; count++){
         if (strcmp(db[count].id,choice)==0) {
            cout.setf(ios::fixed | ios::showpoint);
            cout.precision(2);
            cout << setw(3) << count + 1 << " " << setw(5) << db[count].id << " " << setw(10) << db[count].firstName << " " << db[count].lastName
                 << setw(24) << db[count].email << " " << setw(10) << db[count].salary << endl;
         }
}
return;
}

Last edited on
We would need more information. If this were my program, I would this before line 9 at your function.
cout << db[count].lastName << '\n';

If I were to hazard a guess, it looks like you're asking for a full name, but then only comparing the last name.
It was my intent to only ask for the last name. I'm just trying to figure out why the second function works as intended and the first one doesn't, when they look functionally identically. My guess would be it's some kind of buffer problem, as the "id" in the first one is always 5 digits long and the names are of variable length, but I have no idea what i would need to fiddle with.

Here's the full program:

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
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <string>
#define byID 1
#define byNAME 2
#define dbSIZE 100
#define idSIZE 5
#define nameSIZE 12
#define emailSIZE 20
using namespace std;
struct Employee{
       char id[idSIZE + 1];
       char firstName[nameSIZE + 1];
       char lastName[nameSIZE + 1];
       char email[emailSIZE + 1];
       double salary;
};

void createRecord(char *id, char *firstName, char *lastName, char *email, double salary, Employee db[], int &nEmployees);

void output(Employee db[], int nEmployees) {
     cout.setf(ios::fixed | ios::showpoint);
     cout.precision(2);
     cout << "No. EmpID Employee Name        Email                Salary" << endl;
     cout << "--- ----- -------------------- -------------------- ----------" << endl;
     for (int count = 0; count<nEmployees; count++){
         cout << setw(3) << count + 1 << " " << setw(5) << db[count].id << " " << setw(10) << db[count].firstName << " " << db[count].lastName
              << setw(24) << db[count].email << " " << setw(10) << db[count].salary << endl;
         }
}
void createRecord(char *id, char *firstName, char *lastName, char *email, double salary, Employee db[], int &nEmployees){
     
     strcpy(db[nEmployees].id, id);
	 strcpy(db[nEmployees].firstName, firstName);
	 strcpy(db[nEmployees].lastName, lastName);
	 strcpy(db[nEmployees].email, email);
	 db[nEmployees].salary = salary;
	 
	 nEmployees++;
	 return;
}    
void initialize(Employee db[], int &nEmployees){
     cout.setf(ios::fixed | ios::showpoint);
     cout.precision(2);
     createRecord("00101", "Paula", "Brown","pb@aol.com", 1000, db, nEmployees);
     createRecord("00102", "Paul", "Green","pg@aol.com", 2700.45, db, nEmployees);   
     createRecord("00203", "Chris", "Reddy","cr@aol.com", 2304.67, db, nEmployees);
     createRecord("00204", "Christine", "Bluer","cb@aol.com", 40009.99, db, nEmployees);
     createRecord("00305", "Stephen", "Black", "sb@aol.com", 8500.01, db, nEmployees);
     createRecord("00306", "Wei", "Martin", "wm@aol.com", 98999.99, db, nEmployees);
     createRecord("00307", "Mitch", "Martinez", "mitch@aol.com", 78999.88, db, nEmployees);
     createRecord("00408", "David", "Boyle", "db@aol.com",100000.01, db, nEmployees);
     createRecord("00409", "Chris", "Boyle", "cb@aol.com",200000.01, db, nEmployees);
}
void menu(int& choice, int nEmployees){
     cout << "********** Employee Database ******* Total Number of Employees = " << nEmployees << endl;
     cout << "1. Add an employee" << endl;
     cout << "2. Display all employees" << endl;
     cout << "3. Search employee by ID" << endl;
     cout << "4. Search employee by Last Name" << endl;
     cout << "5. Delete employee by ID" << endl;
     cout << "6. Delete employee by last Name" << endl;
     cout << "7. Exit" << endl;
     cin >> choice;
     if (choice<1 || choice>7){
        cout << "Try again" << endl;
        cin >> choice; }
     }
void input(Employee db[], int &nEmployees){
     char id[6];
     char firstName[13];
     char lastName[13];
     char email[21];
     double salary;
     cin.get();
     cout << "Please input a 5 digit ID" << endl;
     cin.getline(id,6);
     cout << "Please input a first name" << endl;
     cin.getline(firstName,13);
     cout << "Please input a last name" << endl;
     cin.getline(lastName,13);
     cout << "Please input an email" << endl;
     cin.getline(email,21);
     cout << "Please input a salary" << endl;
     cin >> salary;
     createRecord(id, firstName, lastName, email, salary, db, nEmployees);
     return;
}
void idSearch(Employee db[],int nEmployees){
     char choice[6];
     cin.get();
     cout << "Enter search value: ";
     cin.getline(choice,6);
     cout << "No. EmpID Employee Name        Email                Salary" << endl;
     cout << "--- ----- -------------------- -------------------- ----------" << endl;
     for (int count = 0; count<nEmployees; count++){
         if (strcmp(db[count].id,choice)==0) {
            cout.setf(ios::fixed | ios::showpoint);
            cout.precision(2);
            cout << setw(3) << count + 1 << " " << setw(5) << db[count].id << " " << setw(10) << db[count].firstName << " " << db[count].lastName
                 << setw(24) << db[count].email << " " << setw(10) << db[count].salary << endl;
         }
}
return;
}
void nameSearch(Employee db[],int nEmployees){
     char choice[13];
     cin.get();
     cout << "Enter search value: ";
     cin.getline(choice,13);
     cout << "No. EmpID Employee Name        Email                Salary" << endl;
     cout << "--- ----- -------------------- -------------------- ----------" << endl;
     for (int count = 0; count<nEmployees; count++){
         if (strcmp(db[count].lastName,choice)==0) {
            cout.setf(ios::fixed | ios::showpoint);
            cout.precision(2);
            cout << setw(3) << count + 1 << " " << setw(5) << db[count].id << " " << setw(10) << db[count].firstName << " " << db[count].lastName
                 << setw(24) << db[count].email << " " << setw(10) << db[count].salary << endl;
         }
}
return;
}
void idDelete(Employee db[],int &nEmployees){
     char choice[6];
     cin.get();
     cout << "Enter search value: ";
     cin.getline(choice,6);
     cout << "No. EmpID Employee Name        Email                Salary" << endl;
     cout << "--- ----- -------------------- -------------------- ----------" << endl;
     for (int count = 0; count<nEmployees; count++){
         if (strcmp(db[count].id,choice)==0) {
            for (int i = count; i<nEmployees; i++){
                db[i] = db[i-1];
                }
            nEmployees--;
         }
     
}
return;
}
void nameDelete(Employee db[],int &nEmployees){
     char choice[13];
     cin.get();
     cout << "Enter search value: ";
     cin.getline(choice,13);
     cout << "No. EmpID Employee Name        Email                Salary" << endl;
     cout << "--- ----- -------------------- -------------------- ----------" << endl;
     for (int count = 0; count<nEmployees; count++){
         if (strcmp(db[count].lastName,choice)==0) {
            for (int i = count; i<nEmployees; ++i){
                db[i] = db[i-1];
                }
         }
     nEmployees--;
}
return;
}
int main (){
    Employee db[100];
	int nEmployees = 0;
	bool operate = true;
	int choice = 0;
	
	initialize(db, nEmployees);
	while(operate == true){
        menu(choice, nEmployees);
        switch (choice){ 
            case 0:
                 cout << "You done goof'd";
                 break;
            case 1:
                 input(db, nEmployees); 
                 break;
            case 2:
                 output(db, nEmployees);
                 break;
            case 3:
                 idSearch(db, nEmployees);
                 break;
            case 4:
                 idSearch(db, nEmployees);
                 break;
            case 5:
                 idDelete(db, nEmployees);
                 break;
            case 6:
                 nameDelete(db, nEmployees);
                 break;
            case 7:
                 operate = false;
                 break;
     }
}
    system("PAUSE");
    return 0;
}
You aren't calling nameSearch in main(). I didn't read passed that. Doesn't look like a buffer problem, though you should make sure the const char*s passed to create record are the within the length. Your program would break otherwise.

You have those defines at the top of the file, you should use them everywhere (line 126, 144, etc).
Last edited on
Topic archived. No new replies allowed.