Search for a name in a structure

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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
#include <iostream>
#include <iomanip>

using namespace std;

// Making a data type structure of "person"
struct person{

	char name[25];
	char number[13];
	float salary;
	
	};
// Prototyping Procedures
void menu();
void compReport(person temp[], int size);
void condReport(person temp[], int size);
void inputData(person temp[], int &size);
void nameSort(person temp[], int &size);
void salarySort(person temp[], int &size);
void search(person temp[], int &size);



int main()
{
	
	char selection;
	int size = 0;
	person thePeeps[10];
	

	do{
		menu(); // Displaying the menu.
		
		cout << "Please make a selection "<< endl;//Allowing the user to select the option from the menu.
		cin >> selection;
		cin.ignore();

	switch (selection)
	{
		
	case '1':// If selection is 1 the program will allow the user to input more data.

		inputData(thePeeps, size);

		break;

		
	case '2':// if selection is 2 the program will print out a report of the people stored.
		
		compReport(thePeeps, size);

		break;

		
	case '3':// If selection is 3 the program will print a conditional report.

		condReport(thePeeps, size);

		break;
		
	case '4':// If selection is 4 the program will sort the data by name using a bubble sort.
			
		nameSort(thePeeps, size);
		break;

	case '5':
		salarySort(thePeeps, size);
		break;

	case '6':
		search(thePeeps, size);
		break;

		
	case '7':// If selection is 7 the loop will break.
		break;

		
	default: cout << "Invalid Entry" << endl;// The menu will come back up and show this message if the user inputs anything else but a number between 1 and 7.
	
	}

	}while(selection != '7');// If the selection is anything but 7, the loop will continue to run.
	

return 0;

}
void menu()
{
	
	cout << "1) Add new user info " << endl; // Input
	cout << "2) Print complete report " << endl; // All input
	cout << "3) Print conditional report " << endl; // "Greater than"
	cout << "4) Sort the data by name " << endl; // With a bubble sort
	cout << "5) Sort the data by Salary " << endl; // With selection sort
	cout << "6) Search for name " << endl;
	cout << "7) Quit " << endl;
	
	
}
void compReport(person temp[], int size)
{
	cout << setiosflags(ios_base::left) << setw(20) << "Name " << setw(10) << "Phone Number " 
			<< resetiosflags(ios_base::left) << setw(12) << "Salary " <<  endl;

	for(int x = 0; x < size; x++) 
	{
		
		cout << setiosflags(ios_base::left) << setw(20) << temp[x].name << setw(15) << temp[x].number 
			<< resetiosflags(ios_base::left) << setw(10) << setprecision(2) << setiosflags(ios_base::fixed) << temp[x].salary << endl;
	}
}
void condReport(person temp[], int size)
{
	float moneyChoice;

	cout << "Print user with a greater salary than: ";
	cin >> moneyChoice;
	

	cout << setiosflags(ios_base::left) << setw(20) << "Name " << setw(10) << "Phone Number " 
			<< resetiosflags(ios_base::left) << setw(12) << "Salary " <<  endl;
	for (int x = 0; x < size; x++)
	{
		if(moneyChoice < temp[x].salary)
		
			{
				cout << setiosflags(ios_base::left) << setw(20) << temp[x].name << setw(15) << temp[x].number 
				<< resetiosflags(ios_base::left) << setw(10) << setprecision(2) << setiosflags(ios_base::fixed) << temp[x].salary << endl;
			}		
		
	}
}
void inputData(person temp[], int &size)
{
	
	cout << "Enter name: ";
	cin.getline(temp[size].name, 25);
	cout << "Enter Phone Number: ";
	cin.getline(temp[size].number, 13);
	cout << "Enter Salary: ";
	cin >> temp[size].salary;
	size++;
}
void nameSort(person temp[], int &size)
{
	

bool swap = true;

	for(int m = 1; m < size; m++)
	{
	swap = false;

	for(int n = 0; n < size-m; n++)

	{
		int result = strcmp (temp[n].name, temp[n+1].name);

		if (result > 0)
		{
		person swindle = temp[n];
		temp[n] = temp[n+1];
		temp[n+1] = swindle;
		swap = true;
		}
		else if (result == 0)
		{
		swap = false;
		}
		else
		{
		swap = false;
		}
			
	}
			
	}


	}	
void salarySort(person temp[], int &size)
{
	int minIndex;
	float minValue;
	person quick;

	for (int start = 0; start < (size-1); start++) 
	{
		minIndex = start;
		minValue = temp[start].salary;
		quick = temp[start];
		for (int index = start+1; index < size; index++) 
		{     
			if (temp[index].salary < minValue) 
			{	
				quick = temp[index]; // New value copied into quick
				minIndex = index;
			}

		}		
				temp[minIndex] = temp[start]; // Copying the value back
				temp[start] = quick; 
	}                     
}
void search(person temp[], int &size)
{

	
	char searchname;

	cout << "Please enter the name to seach for " << endl;
	cin >> searchname;
	
	for(int y = 0; y < size; y++)
	{
		for(int x = 0; x < size - y; x++)
		{
			int result = strcmp(temp[x].name, searchname);
			
			if (result = 0)
			{     
				cout << setiosflags(ios_base::left) << setw(20) << temp[x].name << setw(15) << temp[x].number 
					<< resetiosflags(ios_base::left) << setw(10) << setprecision(2) << setiosflags(ios_base::fixed) << temp[x].salary << endl;               
			}           
				else                
				{                      
					cout << "Sorry, no student exists with such name" << endl;  
		           
				}   
		}
	}
}

	/*cout << "Please enter a name to search for: " << endl;
	cin >> nameSearch;

	for(int x = 0; x < size; x++)
	{

		if (strcmp(temp[x].name, nameSearch) == 0)

		{
		
		cout << setiosflags(ios_base::left) << setw(20) << temp[x].name << setw(15) << temp[x].number 
			<< resetiosflags(ios_base::left) << setw(10) << setprecision(2) << setiosflags(ios_base::fixed) << temp[x].salary << endl;
		else
			cout << "Name Not Found " << endl;
		}

		


	}
}*/
Last edited on
So the problem I am having is when i get to option 6, my search procedure. I am having difficulty still understanding the strcmp function. Can someone please just give me any help possible. The only error I get is on line 222 (int result = strcmp(temp[x].name, searchname);) that it cannot convert paremeter 2 from char to const char *. I have tried looking up the error online with most answers offering little help. Anything you can give me will help. Thank you.
closed account (zb0S216C)
In ::search(), searchname is not a string, but a single character. Consider the use of std::string.

Wazzak
In class we have not gone over std::string or any thing like that, but i did just look it over and it is hard for me grasp what i am doing. I understand now that it is a single character so the comparison is not going to work how I have it right now, but from what i read i tried to use std::string and I simply got the same error message. "cannot convert paremeter 1 or 2 from string to type char*.
Use the c_str() method of the string to get the corresponding C-string.
I appreciate your (both of you) help with this, but I honestly have no experience with anything string related except I used strcmp() earlier in this project when i was sorting by name. I do not need to use the strcmp if i dont want to, I just figured it was the easiest way but apperently not. Maybe there is another way I can go about this instead. The c_str() method really threw me for a loop because when they explain it they are using pointers and what not that i still have only touched upon and dont fully understand yet either. When i look at how to use them it gets confusing because it turns into layers of information that I only understand bits and pieces of. Does anyone know maybe of a different way to complete this, or maybe a site that might explain one of these options in a little more detail to me. Thank you.
This is where I am going with this now, I have the user enter an array and I need to compare them. I get no error messages but when i walk through it it does not see that they are equal even though they are.

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
void search(person temp[], int &size)
{
	
char searchname[25];

cout << "Please enter the name to seach for " << endl;
cin >> searchname;
	
for(int y = 0; y < size; y++)
{
	for(int x = 0; x < size - y; x++)
	{
	     int result = stricmp(temp[x].name, searchname);
			
	if (result = 0)
	{     
	cout << setiosflags(ios_base::left) << setw(20) << temp[x].name << setw(15) << temp[x].number << resetiosflags(ios_base::left) << setw(10) << setprecision(2) << setiosflags(ios_base::fixed) << temp[x].salary << endl;               
	}           
	else                
               {                      
	cout << "No name found" << endl;  
		           
}   
}
}
}
and yes result was 0 on my watch list, but it did not print out the info in my if statement, it jsut said "No Name Found" 3 times.
Topic archived. No new replies allowed.