Write a simple telephone directory program in C++ that looks up phone numbers in
ID: 3764093 • Letter: W
Question
Write a simple telephone directory program in C++ that looks up phone numbers in a file called directory.txt containing a list of names and phone numbers.
The user should be prompted to enter a first and/or last name. The program should then search the file and print the phone number along with the name entered, or print a message that the name is not in the directory. If only a first name or last name is entered then all entries in the directory matching that first or last name should be printed.
After each look up the program should ask the user if they want to look up another number or exit the program.
The data file, provided by the instructor, is organized so that each line contains a first name, last name and a phone number separated by blank spaces. You can return to the beginning of the file by closing it and opening it again.
Use functional decomposition to solve this problem and code the solution using functions as appropriate. Be sure to use proper formatting and appropriate comments in your code. The output should be labeled clearly and formatted neatly, and the error messages should be informative.
You should create a function called GetInput that will query the user for a first and last name and enter this information into two global string objects. The user must be given the option of enter either the first or last name or both.
You should create a second function called LookUpNumber that will take the first and/or last name, search the directory and print out all matches.
Explanation / Answer
#include 02 #include //Include cin command 03 #include 04 #include 05 #include //Allows for string variables 06 07 using namespace std; 08 void FindName(string, string, string); 09 void PhoneNumber(string); 10 string firstName; 11 string lastName; 12 string searchName; 13 string number; 14 15 int main() 16 { 17 ifstream inFile; //Declares input file of readings 18 inFile.open ("input.txt"); //Opens input file 19 if (!inFile) //Did input file open? 20 { 21 cout > number; 48 searchName=firstName + " " + lastName; 49 50 PhoneNumber(number); 51 } 52 while (inFile.bad()) 53 { 54 coutRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.