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.
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;
}
}