Hello out there.

I have been racking my mind around the below code since Thursday, but I can't seem to figure out why my Search function will not find any values in the map even though I type the exact name from the text file the map is suppose to take into itself.

The Search function will work when I search for a name I just added to the mapList variable, but not for any name from the text file I imported in. Here is the code with the problem, the rest of the files are attached:


Directory.cpp

#include "StdAfx.h"
#include "Directory.h"
#define MaxCharLen 1024
#define _CRT_SECURE_NO_WARNINGS 4996

Directory::Directory(void)
{
}

Directory::~Directory(void)
{
}
void Directory::DisplayMenu(void)
{
	system("CLS");
	cout << "What would you like to do with the Phone Book?\n";
	cout << "1. Add an entry\n";
	cout << "2. Query the Phone Book\n";
	cout << "3. Exit program\n\nEnter your choice: ";
}
bool Directory::ReadFile( std::string &strPathAndFileName )
{
	FILE *pFile = NULL;
	char *token = NULL;
	char tempString[MaxCharLen];
	CMailList TheEntry;
	int i=0;

	// Read data in
	pFile = fopen( strPathAndFileName.c_str(), "r" );
	if(pFile != NULL) {
		while(fgets(tempString,MaxCharLen-1,pFile)) 
		{
			token = strtok(tempString,";");
			if(token[0] != '\n' )
				TheEntry.setname(token);
			else 
				break;
			TheEntry.setstreet(strtok(NULL,";"));
			TheEntry.setcity(strtok(NULL,";"));
			TheEntry.setstate(strtok(NULL,";"));
			TheEntry.setzip(strtok(NULL,";"));
			TheEntry.setphone(strtok(NULL,"\n"));
			i++;
			mapList.insert(std::pair<std::string, CMailList>(TheEntry.getname(), TheEntry));
		}
		fclose(pFile);
		return true;
	}
	else
		return false;
}
bool Directory::WriteFile( std::string &strPathAndFileName )
{
	FILE *pFile = NULL;
	pFile = fopen( strPathAndFileName.c_str(), "w" );
	if(pFile != NULL) {
		// file is open
		std::map<std::string, CMailList>::iterator pIter;
		for(pIter=mapList.begin(); pIter != mapList.end(); pIter++) {
			fprintf(pFile,"%s;%s;%s;%s;%s,%s\n", pIter->second.getname().c_str(),pIter->second.getstreet().c_str(),pIter->second.getcity().c_str(),pIter->second.getstate().c_str(),pIter->second.getzip().c_str(),pIter->second.getphone().c_str());
		}
		fclose(pFile);
		return true;
		
	}
	else
		return false;
}
bool Directory::AddPerson()
{
	CMailList TempList;
	std::string name = "";
	std::string address = "";
	std::string city = "";
	std::string state = "";
	std::string zip = "";
	std::string phone = "";

	//Data is collected
	cout << "Please enter in your name (last name, first name): \n";
	cin >> name;
	cout << "\nPlease enter in your address:\n";
	cin >> address;
	cout << "\nPlease enter in your city: \n";
	cin >> city;
	cout << "\nPlease enter in your state: \n";
	cin >> state;
	cout << "\nPlease enter in your zip code: \n";
	cin >> zip;
	cout << "\nPlease enter in your phone number: \n";
	cin >> phone;

	//Data is inserted into the phone book
	TempList.setname(name);
	TempList.setstreet(address);
	TempList.setcity(city);
	TempList.setstate(state);
	TempList.setzip(zip);
	TempList.setphone(phone);
	mapList.insert(std::pair<std::string, CMailList>(TempList.getname(), TempList));
	return true;
}
bool Directory::Search()
{
	std::string who = "";
	cout << "Enter name: ";
	cin >> who;

	map<std::string, CMailList>::iterator pIter;
	pIter = mapList.find(who);

	if(pIter != mapList.end()) 
		cout << "Phone number: " <<  pIter->second.getphone() << endl;
	else
		cout << "Name not in directory.\n";
	
	system("\npause");
	return true;
}

Try degubbing it. Print out everything in the map before and after you
make that search call.

In Directory::Search(), you get the name using cin. But cin stops at the first whitespace. Since most of the names in address.txt contain some, it will never match.
Have a look at the sticky thread at the top of this forum for some hints on handling console input.

I look into the forums about inputting data, and found that I needed to use cin.ignore() and getline(who) instead of cin >> who so the dang program would accept spaces in the string.

Thanks for the help.

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.