0

So, we have a school-project in creating a phonebook where you should be able to look up phone numbers by searching for the name. I decided to use a map with a string for the phone number and and a vector of strings for the name, due associated number should be able to have multiple names in it.

However, due to us jumping straight from Python to C++ without any explanation of the syntax or the language, I am having a hard time coming up with a way to look for the number by searching for names.

The class I am using looks like this

class Telefonbok
{
    public:
        void add(string namn, string nummer)
        {
            map<string, vector<string>>::iterator it = boken.find(nummer);
            if (it != boken.end())
            {
                cout << "This number already exists, please choose another";
            }
            else
            {
                namn_alias.push_back(namn);
                boken[nummer] = namn_alias;
            }
        }
        void lookup(string name)
        {
            for (map<string, vector<string>>::iterator sokning = boken.begin(); sokning != boken.end(); sokning++)
                cout << "Hello!";
        }
    private:
        vector<string> namn_alias;
        string nummer;
        map<string, vector<string>> boken;
};

What I am trying to do in lookup function is to search for a phone number by the names in the vector, but I am stumped on how to proceed with looking through the vector inside the for-loop.

The plan was to go through the Map keys one by one to find the vector that contains the searched-for name. Any tips on how to proceed or some functions I have missed that can be used for this?

  • "_However, due to us jumping straight from Python to C++ without any explanation of the syntax or the language_" Then, consider reading a [good C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Algirdas Preidžius Nov 14 '17 at 14:01
  • 2
    Don't you need to map the other way around, from name to number? – juanchopanza Nov 14 '17 at 14:08
  • 1
    Your add method is wrong: you probably just need `boken[nummer].push_back(namn);`. – Jarod42 Nov 14 '17 at 14:09
  • You also probably don't want to have either `namn_alias` nor `nummer` as members of `Telefonbok`. `namn_alias` should either be local to the else branch in `add`, or as @Jarod42 suggests not exist. The *member* `nummer` is hidden by the parameter `nummer` in `add`, and not used elsewhere – Caleth Nov 14 '17 at 15:49

1 Answers1

1

Algirdas is correct, you should read up on C++.

Assuming you are mapping name to 1-or-more numbers, but only 1 number per name...

#include <cstddef>
#include <iostream>
#include <map>
#include <string>
#include <vector>

using std::cout;
using std::endl;
using std::map;
using std::string;
using std::vector;

class Telefonbok
{
public:
  void add(string namn, string nummer) {
    auto it = nummer_namn.find(nummer);
    if (it != nummer_namn.end()) {
      cout << "This number already exists, please choose another" << endl;
    }
    else {
      nummer_namn[nummer] = namn;
      namn_nummer[namn].push_back(nummer);
    }
  }

  void lookup(string name) {
    auto it = namn_nummer.find(name);
    if (it == namn_nummer.end()) {
      cout << "Unable to find any numbers for " << name << ", sorry." << endl;
      return;
    }

    for (auto const& sokning : it->second)
      cout << name << " : " << sokning << endl;
  }
private:
  map<string, vector<string>> namn_nummer;
  map<string, string> nummer_namn;
};

int main() {
  Telefonbok bok;
  bok.add("Eljay", "789");
  bok.add("Eljay", "456");
  bok.add("Beaker", "123");

  bok.lookup("Eljay");
  bok.lookup("Beaker");
  bok.lookup("Bunsen Honeydew");

  return EXIT_SUCCESS;
}
Eljay
  • 4,648
  • 3
  • 16
  • 27
  • Thank you. However, the main problem is that each number should have the possibility of multiple names. I wish I had the time to read up on c++, but we had to transcribe everything we did in python (Roughly 3 months of work) over to c++ in less than two weeks. This is the final one, but only three days left. – Tor Nordmark Nov 14 '17 at 14:34
  • My example supports each name having multiple numbers. It should not be too hard for it to be modified for each number to have multiple names. I think StackOverflow generally frowns on giving explicit answers for homework questions... but since you provided a nearly complete code snippet and were having understandable difficulty with C++ syntax, I'm hoping my code example will aid you to finishing your problem. (I think Python makes for a much kinder/gentler language to learn concepts than C++.) – Eljay Nov 14 '17 at 14:43
  • Ah, I see. I will look it over further and hopefully understand it better. Thank you so much for your help! – Tor Nordmark Nov 14 '17 at 14:50