Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Your program will mimic a phone book.You will be given a file. In the file is a

ID: 3831526 • Letter: Y

Question

Your program will mimic a phone book.You will be given a file. In the file is a set of names and phone numbers (an entry).

There will be one item per line, 3

items per entry, you don’t know how many lines are in the file.

You are guaranteed at least one entry.Your program will be menu driven. Your task is to accept a last name, a first name or a phone number from the user and to read through the text file looking for a match. If a match(s)are

found, display the information, and then redisplay the menu.

Ifhe entry was not found display the appropriate not found message.

All search results will be written to the screen.You are guaranteed that when you ask for input you will get the appropriate type not necessarily within range but the right type.

The input file will contain entries. There will be one item per line, all lines in the file end with a carriage return.

Here is a sample input file for clarification:

Steiner

Stuart

509-359-6260

Smith

Brandon

509-359-6260

Roestel

Stacey

509-359-6260

Capaul

Tom

509-359-6260

Steiner

Darren

509-888-7676

SAMPLE OUTPUT:

My Contacts File Program

Please choose from the following:

1) Look up a contact by last name

2) Look up a contact by first name

3) Look up a contact by phone number

4) Quit

Choice------->-3

I am sorry that is not a valid menu choice.

Please try again

Please choose from the following:

1 )Look up a contact by last name

2) Look up a contact by first name

3) Look up a contact by phone number

4) Quit

Choice-->1

Please enter the name you would like to search for: steiner

The information for Steiner is:

Stuart Steiner

509-359-6260

Darren Steiner

509-888-7676

Please choose

from the following:

1)Look up a contact by last name

2) Look up a contact by first name

3) Look up a contact by phone number

4) Quit

Choice-->1

Please enter the name you would like to search for:

peters

The information for Peters

is:

No entries found for that information;

Please choose from the following:

1)Look up a contact by last name

2) Look up a contact by first name

3) Look up a contact by phone number

4) Quit

Choice->4

Thanks for using CSCD1

10 white pages.

Explanation / Answer

C++ code:

#include <bits/stdc++.h>
using namespace std;

int main()
{
   std::map< pair < string, string >, string> record;
   string s1,s2,s3,line;
   ifstream myfile ("phonedata.txt");

   if (myfile.is_open())
   {
   while ( getline (myfile,line) )
   {
       s1 = line;
       getline (myfile,s2);  
       getline (myfile,s3);  
       pair < string, string > mypair;
       mypair.first = s1;
       mypair.second = s2;
       record[mypair] = s3;
   }
   myfile.close();
   }
   else
   {
   cout << "Unable to open input file" << endl;
   exit(1);
   }

   while(true)
   {
       int c;
       cout << "Please choose from the following: 1) Look up a contact by last name 2) Look up a contact by first name 3) Look up a contact by phone number 4) Quit" << endl;
       cin >> c;
       if(c == 1)
       {
           cout << "Please enter the name you would like to search for: ";
           string surname;
           cin >> surname;          
       int m = 0;
       for (std::map<pair < string, string >, string>::iterator it=record.begin(); it!=record.end(); ++it)
       {
               if((it->first).first == surname)
               {
                   cout << (it->first).first << " " << (it->first).second << " " << it->second << endl;
                   m++;
               }
       }
       if(m == 0)
       {
           cout<< "No entries found for that information"<< endl;
       }
       }
       else if(c == 2)
       {
           cout << "Please enter the name you would like to search for: ";
           string name;
           cin >> name;
           int m = 0;
           for (std::map<pair < string, string >, string>::iterator it=record.begin(); it!=record.end(); ++it)
           {
                   if((it->first).second == name)
                   {
                       cout << (it->first).first << " " << (it->first).second << " " << it->second << endl;
                       m++;
                   }
           }
           if(m == 0)
           {
               cout<< "No entries found for that information"<< endl;
           }
       }
       else if(c == 3)
       {
           cout << "Please enter the phone number you would like to search for: ";
           string phonen;
           cin >> phonen;
           int m = 0;
           for (std::map<pair < string, string >, string>::iterator it=record.begin(); it!=record.end(); ++it)
           {
                   if(it->second == phonen)
                   {
                       cout << (it->first).first << " " << (it->first).second << " " << it->second << endl;
                       m++;
                   }
           }
           if(m == 0)
           {
               cout<< "No entries found for that information"<< endl;
           }
       }
       else if (c == 4)
       {
           exit(1);
       }
       else
       {
           cout << "I am sorry that is not a valid menu choice.";
       }
   }          
   return 0;
}

Sample Output:

Please choose from the following:
1) Look up a contact by last name
2) Look up a contact by first name
3) Look up a contact by phone number
4) Quit
1
Please enter the name you would like to search for:
Steiner
Steiner Darren
509-888-7676
Steiner Stuart
509-359-6260
Please choose from the following:
1) Look up a contact by last name
2) Look up a contact by first name
3) Look up a contact by phone number
4) Quit
2
Please enter the name you would like to search for:
akash
No entries found for that information
Please choose from the following:
1) Look up a contact by last name
2) Look up a contact by first name
3) Look up a contact by phone number
4) Quit
3
Please enter the phone number you would like to search for:
509-888-7676
Steiner Darren
509-888-7676
Please choose from the following:
1) Look up a contact by last name
2) Look up a contact by first name
3) Look up a contact by phone number
4) Quit
4