1

Problem Statement

Given names and phone numbers, assemble a phone book that maps friends' names to their respective phone numbers. You will then be given an unknown number of names to query your phone book for. For each name queried, print the associated entry from your phone book on a new line in the form "name=phoneNumber"; if an entry for name is not found, print "Not found" instead.

Input Format: ...

After the lines of phone book entries, there are an unknown number of lines of queries. Each line (query) contains a to look up, and

You must continue reading lines until there is no more input.

How should I loop it until there is no more input?

Also can someone tell me how this is possible in C++?

Here's my code in Python 3:

n = int(input())
names = {}
for foo in range(n):
    entry = input().split(' ')
    names[entry[0]] = entry[1]
while (1==1):
    check = input()
    if(names.get(check)!=None):
        print(check + '=' + names.get(check))
    else:
        print('Not Found')

It just loops infinitely and therefore triggers the error. enter image description here

Here's the C++ code:

#include<iostream>
#include<map>
#include<string>
using namespace std;

int main(void)
{
    map<string, string> phonebook;
    int n;
    cin >> n;
    string key, num;
    for(int i = 0; i < n; i++)
    {
        cin >> key >> num;
        phonebook.insert(pair<string, string>(key, num));
    }
    while (1 == 1)
    {
        cin >> key;
        if(phonebook.count(key) > 0)
            cout << key << "=" << phonebook[key] << endl;
        else
            cout << "Not found" << endl;
    }
}
Harshit Joshi
  • 260
  • 4
  • 15
  • For future reference, please constrain your question to only one issue. Otherwise, your question may be closed as too broad or unclear. Thanks. – TrebledJ Mar 16 '19 at 13:25
  • @TrebuchetMS So should I post them as two separate questions? – Harshit Joshi Mar 16 '19 at 13:27
  • I'd do a bit more research first before posting both (or either or any) question. Googling "_reading lines until there is no more input_" isn't too hard, is it? :-) But yes, if it comes down to it, it's better to post two separate questions. (Or consider posting one question and attempting to solve the other one on your own.) – TrebledJ Mar 16 '19 at 13:30

2 Answers2

2

How should I loop it until there is no more input?

Your use of the while loop is appropriate. To catch and silence the error, you can use a try-except block:

n = int(input())
names = {}
for foo in range(n):
    entry = input().split(' ')
    names[entry[0]] = entry[1]

while True:     # (1 == 1) == True
    try:
        check = input()
    except EOFError:  # catch the error
        break       # exit the loop

    if(names.get(check)!=None):
        print(check + '=' + names.get(check))
    else:
        print('Not Found')

Also can someone tell me how this is possible in C++?

Hmm... weird request. I'll point you to std::getline and std::map and let them do the talking. :-)

TrebledJ
  • 8,713
  • 7
  • 26
  • 48
  • @harshit54 There's a way to do it without catching: [how to read until eof from cin in C++](https://stackoverflow.com/questions/201992/how-to-read-until-eof-from-cin-in-c). – TrebledJ Mar 16 '19 at 13:24
  • 1
    Sorry about that. I did try googling this exact same question but could not find any answer. I will remember to search more thoroughly next time. I do remember using while(getline()) to read until EndOfFile while using file streams. I didn't know that it was valid for the input stream as well. – Harshit Joshi Mar 16 '19 at 13:41
0

Here's the correct C++ code:

#include<iostream>
#include<map>
#include<string>
using namespace std;

int main(void)
{
    map<string, string> phonebook;
    int n;
    cin >> n;
    string key, num;
    for(int i = 0; i < n; i++)
    {
        cin >> key >> num;
        phonebook.insert(pair<string, string>(key, num));
    }
    getline(cin, key);

    while(getline(cin, key))        //Loop runs while we are getting input.
    {
        if(phonebook.count(key) > 0)
            cout << key << "=" << phonebook[key] << endl;
        else
            cout << "Not found" << endl;
    }
}
Harshit Joshi
  • 260
  • 4
  • 15