Phone Book

I was wondering if you could help me about this. It's a phone book. I am recording name in it. Then searching it from number and from name.

Problems;

1- Search from name: it finds the first search then when select opt2 second time it can't find anything and goes to the main menu.

2- Search from number: It asks me to type the number and goes to the main menu.

3-I also realized when I use 100 instead of "count" in for loop it works fine ( but in first try onlt). So I did the same thing in phone. So I have said for ( int i =0 ; i < 100 ; i++){ for both loops. I thought it should be working for either count or 100. But it seems there is a difference. It's not working in properly even I type 100.

#include <fstream>
#include <iostream>
#include <string>

using namespace std;

void addName(string names[], string phones[], int count)
{

string name;
cout << "Enter a name:";
cin >> name;

string phone;
cout << "Enter a phone number:";
cin >> phone;

names[count] = name;
phones[count] = phone;
count++;

}

void searchName(string names[], string phones[], int count)
{
string name;
cout << "Enter Name:";
cin >> name;

for (int i = 0; i < 100; i++){

if (name == names[i])
{
cout << names[i] << " 's phone number is: " << phones[i] << endl << endl;
}

}
}

void searchPhone(string names[], string phones[], int count){

string phone;
cout << "Enter a phone number: ";
cin >> phone;

for (int i = 0; i < 100; i++)
{
if (phone == phones[i])
{
cout << phones[i] << "belongs to " << names[i];
}

}

}
int main()
{
string names[100];
string phones[100];
int count = 0;

while (true)
{
cout << "Ultimate Phone Book v1.2" << endl;
cout << "1- Add a Record" << endl;
cout << "2- Search By Name" << endl;
cout << "3- Search By Phone" << endl;
cout << "4- Display All" << endl;
cout << "0- Exit" << endl << endl;

int choice;

cout << "What's your choice?:";
cin >> choice;

cout << endl;


if (choice == 1) // Add Names
{
addName(names, phones, count);
}

if (choice == 2) //Search Names
{
searchName(names, phones, count);
}

if (choice == 3) //Search By Phone
{
searchPhone(names, phones, count);
}





}
}
can you please use code tags? It makes it easier to read.
http://www.cplusplus.com/forum/articles/42672/

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
#include <fstream>
#include <iostream>
#include <string>
#include <windows.h>
using namespace std;
string names[100];
string phones[100];
int count = 0;
void addName()
{

string name;
cout << "Enter a name:";
cin >> name;

string phone;
cout << "Enter a phone number:";
cin >> phone;

names[count] = name;
phones[count] = phone;
count++;

}

void searchName()
{
string name;
cout << "Enter Name:";
cin >> name;

for (int i = 0; i < 100; i++){

if (name == names[i])
{
cout << names[i] << " 's phone number is: " << phones[i] << endl << endl;
}

}
}

void searchPhone()
{

string phone;
cout << "Enter a phone number: ";
cin >> phone;

for (int i = 0; i < 100; i++)
{
if (phone == phones[i])
{
cout << phones[i] << "belongs to " << names[i];
}

}

}
int main()
{
while (true)
{
cout << "Ultimate Phone Book v1.2" << endl;
cout << "1- Add a Record" << endl;
cout << "2- Search By Name" << endl;
cout << "3- Search By Phone" << endl;
cout << "4- Display All" << endl;
cout << "0- Exit" << endl << endl;

int choice;

cout << "What's your choice?:";
cin >> choice;

cout << endl;


if (choice == 1) // Add Names
addName();
else if (choice == 2) //Search Names
searchName();
else if (choice == 3) //Search By Phone
searchPhone();
system("pause");
system("CLS");
}
}

I made all your variables global, I added a clear screen effect when the program restarts, and I changed the way your if statements were set up.
Last edited on
Thanks I didn't know it.

Thank you very much for replying it. I am very curious what was wrong with my code and how would you do that without making global variable?

Best
Kyre
I just made all the variables global for my own betterment. I have the program even more progressed here:
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
#include <fstream>
#include <iostream>
#include <string>
#include <windows.h>
using namespace std;
string names[100];
string phones[100];
int count = 0;
void addName()
{

string name;
cout << "Enter a name:";
cin >> name;

string phone;
cout << "Enter a phone number:";
cin >> phone;

names[count] = name;
phones[count] = phone;
count++;

}

void searchName()
{
string name;
cout << "Enter Name:";
cin >> name;

for (int i = 0; i < 100; i++){

if (name == names[i])
{
cout << names[i] << " 's phone number is: " << phones[i] << endl << endl;
}

}
}

void searchPhone()
{

string phone;
cout << "Enter a phone number: ";
cin >> phone;

for (int i = 0; i < 100; i++)
{
if (phone == phones[i])
{
cout << phones[i] << "belongs to " << names[i];
}

}

}

void DisplayContact()
{
for(int i=0; i<=names.length();i++) ///Broken On Purpose BTW
{
cout<<names[i]<<"  "<<phones[i]<<endl;
}

}

int main()
{
while (true)
{
cout << "Ultimate Phone Book v1.2" << endl;
cout << "1- Add a Record" << endl;
cout << "2- Search By Name" << endl;
cout << "3- Search By Phone" << endl;
cout << "4- Display All" << endl;
cout << "0- Exit" << endl << endl;

int choice;

cout << "What's your choice?:";
cin >> choice;

cout << endl;


if (choice == 1) /// Add Names
addName();
else if (choice == 2) ///Search Names
searchName();
else if (choice == 3) ///Search By Phone
searchPhone();
else if (choice == 4)
DisplayContact();
else if (choice == 0)
return 0;
system("pause");
system("CLS");
}
}

I left the contact displaying part broken on purpose BTW. I'll let you finish it.
Last edited on
I appreciate for the effort. Meanwhile I found my mistake. Which was about "reference by value" After I've put "&" front of the count everything worked well.

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

using namespace std;

void addName(string names[], string phones[], int &count)
{

	string name;
	cout << "Enter a name:";
	cin >> name;

	string phone;
	cout << "Enter a phone number:";
	cin >> phone;

	names[count] = name;
	phones[count] = phone;
	count++;

}

void searchName(string names[], string phones[], int &count)
{
	string name;
	cout << "Enter Name:";
	cin >> name;

	for (int i = 0; i < count; i++){

		if (name == names[i])
		{
			cout << names[i] << " 's phone number is:   " << phones[i] << endl << 
Last edited on
nice
Topic archived. No new replies allowed.