phonebook search function

hi! I have an assignment that requires constructing a phonebook directory. I have a problem with the search option. I'm not quite sure what the problem is with the code. Please help, thanks!

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
#include<iostream>
#include<string>

using namespace std;

int phoneEntry (string names[], string numbers[])
{string entryNames, entryNumbers;
int entry = 0;
while ((entry <10) && entryNames != "finish")
{
	cout <<endl;
	cout<<endl;
	cout<<"Please enter name: ";
	getline(cin, entryNames);
 if (entryNames != "finish")
 { names[entry] = entryNames;
 cout<< "Please enter phone number: " ;
 getline (cin, numbers[entry]);
 cout<<endl;
 cout<<endl;
 entry++;
 }
}
return entry;
}



int minimum (string names[], int maxEntries, int initial)
{ int start;
int pos = initial;
for (start = initial+1; start<maxEntries; start++)
{if (names[start]<names[pos])
pos = start;
}
return pos;
}



void swap (int& a, int& b)
{ int temp;
temp = a;
a=b;
b=temp;
return;
}



void arrange (string names[], string numbers[], int maxEntries)
{
	int arr;
	for(int i=0; i<maxEntries-1; i++)
	{ arr = minimum(names, maxEntries, i);
	swap(names[i], names[arr]);
	swap(numbers[i], numbers[arr]);
	}
}



int search (string names[], int maxEntries, string key)
{ int i=0;
int place;
while ((i<maxEntries) && (names[i] != key))
	i++;
if (i != maxEntries)
	place = i;
else
	place = -1;
return place;
}


void phEntry (string names[], string numbers[], int& maxEntries)
{ string entryName, entryNumber;
int entry = maxEntries;
if (entry >=10 ) 
{cout << "Maximum number of entries is only 10. " <<endl;
cout<<endl;
cout<<endl;
}
else {
	cout <<"Please enter name: ";
	getline(cin, names[entry]);
	cout<<"Please enter phone number: ";
	getline(cin, numbers[entry]);
	entry++;
	maxEntries = entry;
	arrange(names, numbers, maxEntries);
}
}


void menu (string names[], string numbers[], int maxEntries)
{string key, select;
int i;
while (select != "d")
{cout <<"Please select from the following (enter letter of choice): ";
cout<<"(a)display  (b)add  (c)search  (d)quit ::  " ;
cin>>select;

if (select == "a")
{ for (int i=0; i<maxEntries; i++)
cout << names[i] << " - " << numbers[i] << endl; 
cout<<endl;
}
else if (select == "b")
	phEntry(names, numbers, maxEntries);
else if (select == "c") 
{ cout <<"Please enter person's name: " ;
getline(cin, key);
i = search(names, maxEntries, key);
if (i == -1)
	cout <<"No entry found";
else cout<< names[i] << " - phone number: " << numbers[i] ;
}
}
}



int main()
{int maxEntries=0;
string names[10], numbers[10], key;
cout <<"Please make an entry. Enter finish to quit.";
cout<<endl;
cout<<endl;
maxEntries = phoneEntry(names, numbers);
arrange(names, numbers, maxEntries);
menu(names, numbers, maxEntries);
search (names, maxEntries, key);

return 0;
}


Last edited on
clean up your code its a little hard to read.
Topic archived. No new replies allowed.