3 Replies - 1624 Views - Last Post: 10 June 2010 - 03:14 PM Rate Topic: -----

#1 Guest_smerre01*


Reputation:

Phone Number Lookup Code Error Message Help

Posted 10 June 2010 - 01:28 PM

Hello I am a student in a C++ online class trying to independently learn C++. Things have been going well so far, I am now stuck on my week 6 assignment. The assignment is:

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.

Is This A Good Question/Topic? 0

Replies To: Phone Number Lookup Code Error Message Help

#2 Guest_smerre01*


Reputation:

Re: Phone Number Lookup Code Error Message Help

Posted 10 June 2010 - 01:28 PM

Hello I am a student in a C++ online class trying to independently learn C++. Things have been going well so far, I am now stuck on my week 6 assignment. The assignment is:

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.
Was This Post Helpful? 0

#3 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1638
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Phone Number Lookup Code Error Message Help

Posted 10 June 2010 - 01:34 PM

Topics merged, please dont create duplicates :)
Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6036
  • View blog
  • Posts: 23,422
  • Joined: 23-August 08

Re: Phone Number Lookup Code Error Message Help

Posted 10 June 2010 - 03:14 PM

I can see an if statement being important here. You might want to look up what happens if the [] operator of a std::map does not find the key. Maybe that's not really the operator you want to use?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1