Phone Number List C++

closed account (i8516Up4)
Write a simple telephone directory program in C++ that looks up phone numbers in a file containing a list of names and phone numbers. The user should be prompted to enter a first name and last name, and the program then outputs the corresponding number, or indicates that the name isn't in the directory. After each lookup, the program should ask the user whether they want to look up another number, and then either repeat the process or exit the program. The data on the file should be organized so that each line contains a first name, a last name, and a phone number, separated by blanks. You can return to the beginning of the file by closing it an opening it again.

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

using namespace std;

int main()
{
const int SIZE = 50;
int size = 0;

// Array of product descriptions
char phoneDirectory[SIZE];

char name; // For user input
char *strPtr = NULL; // Result from strstr

ifstream inFile;
inFile.open("phonebook");
while (!inFile.fail())
{
cin.getline(inFile,phoneDirectory[size]);
size++;
}
inFile.close();

// Get user input
cout << "Enter a name to search for: ";
cin.getline(name, SIZE);

// Search for the string
int index = 0;
while(index < size)
{
strPtr = strstr(phoneDirectory[index], name);
if (strPtr != NULL)
break;
index++;
}

// Output the result of the search
if (strPtr == NULL)
cout << "No matching names were found.\n";
else
cout << phoneDirectory[index] << endl;
return 0;
}

I cant seem to fix the cin.getline's and the strstr.
Last edited on
Are you expected to write your own linked list or can you use one that the standard library provides?
You have declared name to be a single character. You want a string.
Get this code working and then consider what happens if the phonebook contains more than 50 entries. A more reliable way to do this program is to prompt for the name and then read the file. Compare each line of the file to the name and then throw it away if it doesn't match.

Since you will have to loop and do the whole lookup several times, consider putting it in a function. For now your main() can just call the function once. When you have it working, add the code to call the function inside a loop.
Start with something like that and then add your logic

your file : Contacts.txt in your project directory
1
2
Steve Jobs 123456
Ericool . 124563


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

struct Contact
{
	std::string m_fName;
	std::string m_lName;
	int m_pNumber;

	Contact(const char* firstName, const char* lastName, int phoneNumber)
		: m_fName(firstName)
		, m_lName(lastName)
		, m_pNumber(phoneNumber)
	{
	}
};
class ContactList
{
	std::vector<Contact> m_myContacts;
public:
	ContactList()
		: m_myContacts()
	{
	}
	void addContact(Contact contact)
	{
		m_myContacts.push_back(contact);
	}
	friend std::ostream& operator << (std::ostream& os , const ContactList& list)
	{
		for (size_t i = 0; i < list.m_myContacts.size(); ++i)
		{
			os << list.m_myContacts[i].m_fName 
				<< " " << list.m_myContacts[i].m_lName 
				<< " : " << list.m_myContacts[i].m_pNumber 
				<< std::endl;
		}
		return os;
	}
};

int main(int argc, char* argv[])
{
	std::ifstream streamReader("Contacts.txt", std::ios::in);

	ContactList myContactList;

	if (!streamReader.good())
	{
		std::cout << "Was not able to read the file\n";
		return 0;
	}

	while (!streamReader.eof())
	{
		std::string firstName("");
		std::string lastName("");
		int phoneNumber = -1;

		streamReader >> firstName >> lastName >> phoneNumber;

		Contact contact(firstName.c_str(), lastName.c_str(), phoneNumber);

		myContactList.addContact(contact);

	}
        streamReader.close();

	//do your stuff

	//prints to console all contacts
	std::cout << myContactList;

	return 0;
}
Ericool You shouldn't do homework, just offer help. In the end, it doesn't help anyone.
I 'm trying..
Topic archived. No new replies allowed.