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.
Use functional decomposition to solve the 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 clearly labeled and neatly formatted, and the error messages should be informative.
I was all ready to submit it in, but then saw the part about the error message requirement for names that are not in the directory. Now I am scrambling trying to figure out how to implement that into my current code.
#include <iostream>
#include <cstdlib>
#include <string>
#include <fstream>
#include <map>
using namespace std;
typedef int PhNum;
PhNum GetNumber(string Name);
string GetName(PhNum Number);
void readNamesAndNumbers();
map<string,PhNum> PhoneNumbers;
int main(void)
{
string nameToFind;
readNamesAndNumbers();
do
{
cout << "Input the first and last name to lookup: ";
string lastname, firstname;
cin >> firstname >> lastname;
nameToFind =firstname + " " + lastname;
cout << nameToFind << "\'s number is: " << GetNumber(nameToFind) << endl;
cout << "enter another name (y or yes anything else is no) ? ";
cin.get(); // remove last <enter> key
}
while(cin.get() == 'y'); // if y read again
cin.get();cin.get();
return EXIT_SUCCESS;
}
void readNamesAndNumbers ()
{
ifstream phoneFile;
string Name;
PhNum Number;
phoneFile.open("DIR.txt");
while (phoneFile.good())
{
string lastname, firstname;
phoneFile >> firstname >> lastname >> Number;
Name=firstname + " " + lastname;
PhoneNumbers[Name] = Number;
}
}
PhNum GetNumber(string Name)
{
return PhoneNumbers[Name];
}
string GetName(PhNum Number)
{
map<string,PhNum>::iterator PNItr;
for(PNItr = PhoneNumbers.begin();PNItr != PhoneNumbers.end();PNItr++)
if(PNItr->second == Number)
return PNItr->first;
return "";
}
Any examples or simplified explainations I can get would be great. I have really been busting my hump to learn this, so I have been putting in the effort, but now I have simply ran out of time this week for the assignment. I greatly appreciate everyone who contributes here as it has been a good resource for me to search thru.

New Topic/Question
Reply
MultiQuote







|