A simple telephone directory program in C++ that looks up phone numbers in a fil
ID: 3625438 • Letter: A
Question
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 and last name, and the program should either output the corresponding number or indicate that the name isn't present in the directory. After each lookup, the program should ask the user whether he or she wants 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, last name, and a phone number, separated by blanks. You can return to the beginning of the file by closing it and opening it again.Explanation / Answer
please rate - thanks
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
void lookup(string,string,char[]);
int main()
{char filename[30];
string first,last;
char yorn;
cout<<"what is the name of the file you are using? ";
cin>>filename;
do
{
cout<<"Enter first name: ";
cin>>first;
cout<<"Enter last name: ";
cin>>last;
lookup(first,last,filename);
cout<<"lookup another number(Y/N): ";
cin>>yorn;
}while(toupper(yorn)=='Y');
return 0;
}
void lookup(string f,string l,char filename[])
{ifstream input;
string first,last,phone;
bool done=false;
input.open(filename); //open file
if(input.fail()) //is it ok?
{ cout<<"file did not open please check it ";
system("pause");
exit(1);
}
input>>first;
while(input&&!done)
{input>>last>>phone;
if(f.compare(first)==0&&l.compare(last)==0)
{cout<<f<<" "<<l<<" "<<phone<<endl;
done=true;
}
input>>first;
}
input.close();
input.clear();
if(!done)
cout<<f<<" "<<l<<" not found in the directory"<<endl;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.