Currently im creating a simple phone directory. I am having a problem when searching the vector. It only lets me search for the exact key in the directory when I need to to search for strings that are close to the name. For example when I search "car" it will state that its not in the directory when it should pull up Carr, Derek and Carr David. Please help Thank you.

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

int main()
{
std::map<std::string, std::vector <std::string> > directory;

directory["Carr"].push_back("Derek 916-667-6761");
directory["Carr"].push_back("David 916-667-6766");
directory["Mcfadden"].push_back("Darren 510-667-0000");
directory["Streater"].push_back("Rod 510-667-0001");
directory["Jones"].push_back("James 510-667-0020");
directory["Mack"].push_back("Khalil 707-557-6700");
directory["Woodson"].push_back("Charles 707-557-0061");
directory["Tuck"].push_back("Justin 707-557-6001");
directory["Moore"].push_back("Sio 415-600-5551");
directory["Roach"].push_back("Nick 415-600-4461");
directory["Woodson"].push_back("Rod 415-600-4441");

std::string str;
int yesno;
std::cout <<"Would you like to search a name in the phone directory?(yes=1, no=0)" <<std::endl;
std::cin>>yesno;
while (yesno==1)
{
    std::cout << "Enter name you would like to find: ";
    std::cin >> str;

    std::map<std::string, std::vector <std::string> >::iterator p;

    p = directory.find(str);
    if(p != directory.end())
    {
      std::string key = p->first;
      std::vector<std::string> names = p->second;

      for (int i = 0; i < names.size(); ++i)
         std::cout << key << " " << names[i] << std::endl;

    }
    else
    {
        std::cout << "Name not in phone directory.\n";
    }
   std::cout <<"Do you have another name you would like to look up?(yes=1, no=0)"<<std::endl;
   std::cin>>yesno;
   }
   system("pause");
   return 0;
  }

Recommended Answers

All 3 Replies

when I search "car"

Maybe you should try searching for "Carr".

A map is not case-insensitive by default. You need to specify your own comparison function in the template argument list for the directory object to handle your use case. Here is a link to the documentation for std::map: http://www.cplusplus.com/reference/map/map/

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.