-3
\$\begingroup\$

Problem description:

You are given a phone book that consists of your friend's names and their phone number. After that you will be given your friend's name as query. For each query, print the phone number of your friend.

The first line will have an integer N denoting the number of entries in the phone book. Each entry consists of two lines: a name and the corresponding phone number.

After these, there will be some queries. Each query will contain name of a friend. Read the queries until end-of-file.

For each case, print "Not found" without quotes, if the friend has no entry in the phone book. Otherwise, print the friend's name and phone number. See sample output for the exact format.

Sample input:

3
sam
99912222
tom
11122222
harry
12299933
sam
edward
harry

Sample output:

sam=99912222
Not found
harry=12299933

My solution:

int main() 
   {    
    map<string, string> PhoneList;

    int n;

    string name,ph,str;

    while(n--)

        {

         cin>>name>>ph;

         PhoneList[name] = ph;

        }

    while( getline (cin,t) )

      { 
          cin>>str;

          auto it = PhoneList.find(str);

               if(it==PhoneList.end())

                  cout<<"Not Found\n";

               else

                  cout<<it->second;
      }

    return 0;
}
\$\endgroup\$

closed as off-topic by Edward, Quill, ChrisWue, Ethan Bierlein, Morwenn Jan 9 '16 at 0:20

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions containing broken code or asking for advice about code not yet written are off-topic, as the code is not ready for review. After the question has been edited to contain working code, we will consider reopening it." – Edward, Quill, ChrisWue, Ethan Bierlein, Morwenn
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    \$\begingroup\$ Welcome to Code Review! I'm afraid this question does not match what this site is about. Code Review is about improving existing, working code. Code Review is not the site to ask for help in fixing or changing what your code does. Once the code does what you want, we would love to help you do the same thing in a cleaner way! Please see our help center for more information. \$\endgroup\$ – Edward Jan 8 '16 at 17:38
  • 3
    \$\begingroup\$ Does it work as intended? Are you interested in feedback on any & all aspects of the code? Please edit to clarify your post. \$\endgroup\$ – Mathieu Guindon Jan 8 '16 at 17:42
  • \$\begingroup\$ The code as it stands doesn't compile - and even if it would it wouldn't match the given specification. So it's definitely off-topic I'd say \$\endgroup\$ – ChrisWue Jan 8 '16 at 23:43
  • \$\begingroup\$ @Edward it does not work properly .. like i had problems with reading strings until eof ... i got it now... thank you !! \$\endgroup\$ – Sindhuja C S Jan 9 '16 at 2:44
  • \$\begingroup\$ @Mat'sMug yeah i jus had doubts regarding the reading until eof ... thank you ! \$\endgroup\$ – Sindhuja C S Jan 9 '16 at 2:45
2
\$\begingroup\$

There are several issues with your program, some of them rendering it non-functional. As it is a very simple one, it does not take much time to fix it, but as already specified by Edward, you should submit only working code that requires reviewing.

1) Code aspect

You should try to keep you code as organized and homogeneous as possible. Usually, it is not required for homework, but it is good in a programmer's life:

int main() 
   {    
    map<string, string> PhoneList;

    int n;

should be properly formatted like

int main() 
{    
   map<string, string> PhoneList;

   int n;

Try to leave blanks between operators and operands, as this will increase readability:

string name,ph,str;

should be replaced with

string name, ph, str;

2) Variable declaration closer to usage

C++ allows you to declare your variables anywhere before they are actually used and it is a good idea to be as close as possible. The first part of your program may look like this:

int n;
cout << "Provide number of entries: ";
cin >> n;

map<string, string> PhoneList;
while (n --)
{
    string name, phone;

    // this is added just to see something relevant on the screen - it might be removed
    cout << "Name " << (n + 1) << ":\n";
    cin >> name;

    cout << "Phone " << (n + 1) << ":\n";
    cin >> phone;

    PhoneList[name] = phone;
}

3) getLine requires a cin.ignore() before, as it will catch a newline for a previously entered phone (at least when testing manually)

Finally, a version of your program with all improvements made:

// Example program
#include <iostream>
#include <string>
#include <map>

using namespace std;

int main() 
{    
    int n;
    cout << "Provide number of entries: ";
    cin >> n;

    map<string, string> PhoneList;
    while (n --)
    {
        string name, phone;

        cout << "Name " << (n + 1) << ":\n";
        cin >> name;

        cout << "Phone " << (n + 1) << ":\n";
        cin >> phone;

        PhoneList[name] = phone;
    }

    string str;
    cin.ignore();
    while(getline(cin, str))
    { 
        auto it = PhoneList.find(str);
        if (it == PhoneList.end())
            cout << "Not Found\n";
        else
            cout << it->second << "\n";
    }
}

This is tested in cpp.sh

\$\endgroup\$
  • \$\begingroup\$ ...i ll make sure of the format next time ! \$\endgroup\$ – Sindhuja C S Jan 9 '16 at 2:50
  • \$\begingroup\$ is this the best way of doing this ? I mean ...can we optimise it further? \$\endgroup\$ – Sindhuja C S Jan 12 '16 at 15:41

Not the answer you're looking for? Browse other questions tagged or ask your own question.