STL Map problems

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

36 Replies - 3449 Views - Last Post: 19 November 2011 - 09:38 PM Rate Topic: -----

#1 needhelp101   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 60
  • Joined: 18-November 11

STL Map problems

Posted 18 November 2011 - 10:26 PM

My objective to is create a phone book that will receive a name, category, phone number, and address from the user. after the user enters their info, I would like their data to be saved. My code compiles, but i am having problems with some parts of my program. my find, report, and test functions do not work. The find function is suppose to search for a name in the phone book. for example, the user can enter in info for 3 people, and wants to find the info for one of the people entered by the user. the report function is to display all of the people entered into the phone book. the test function is supposed to generate 10 random people, that have a random name, category, phone number, and address, and display them on the screen. Also, my add function does not seem to work properly either. It just does not seem to be saving any new people the user adds to the phone book. I will include all of my files. There will be 9 files altogether.

//listing.h

#pragma once
#include "Person.h"
#include <vector>
using namespace std;

enum SortStatus {unsorted,sortedByName,sortedByCode};

class listing
{
public:
	unsigned int size() { return vectorOfPerson.size();}	
	Person getPerson(int i);
	void clear(){sortStatus=unsorted;vectorOfPerson.clear();}
	void customerList();
	void addNewListing(string n, string c, string p, string a);
private:
	SortStatus sortStatus;
	vector<Person> vectorOfPerson;
};


//listing.cpp


#include "listing.h"
#include <algorithm>
#include <map>


void listing::addNewListing(string n, string c, string p, string a)
{
	map<string,Person> phoneData;
	Person data(n,c,p,a);
	phoneData[n]=data;
}

Person listing::getPerson(int i)
{
	return vectorOfPerson[i];
}


//misc.h


#pragma once
#include <string>
using namespace std;

void stringToLower(string & s);
void stringToUpper(string & s);

string padLeft(string s, char fill,  unsigned int size);
string padRight(string s,char fill,  unsigned int size);

string intToString(int x);
int stringToInt(string s);

string intToDollarString(int x);

string randStrings(int numOfChars);
int randInt(int lower,int upper);


//misc.cpp


#include "misc.h"

void stringToLower(string & s)
{
	for(unsigned int i=0;i<s.length();i++)
	{
		s[i]=tolower(s[i]);
	}
}

void stringToUpper(string & s)
{
	for(unsigned int i=0;i<s.length();i++)
	{
		s[i]=toupper(s[i]);
	}
}


string padLeft(string s,char fill, unsigned int size)
{
	while(s.length()<size)
	{
		s= fill + s;
	}
	return s;
}

string padRight(string s,char fill,  unsigned int size)
{
	while(s.length()<size)
	{
		s= s + fill;
	}
	return s;
}

//All of the following would work fine.
//The first two give warnings. Try them out and read the warnings.
//
//itoa(x,temp,10);
//_itoa(x,temp,10);
//_itoa_s(x,temp,255,10);
//
//The last one is "safe" because it's guaranteed to not go beyond 255 characters.
//In this case it can't happen but it would be safe even if temp was very small array as long as the third parameter was also appropriately small.

string intToString(int x)
{
	string result;
	char temp[256];
	//itoa(x,temp,10);
	//_itoa(x,temp,10);
	_itoa_s(x,temp,255,10);
	result=temp;
	return result;
}

int stringToInt(string s)
{
	return atoi(s.c_str());
}





string intToDollarString(int x)
{
	string result=intToString(x);
	result=padLeft(result,'0',3);
	result.insert(result.length()-2,".");
	return result;
}


//'A' is a char literal for the ascii value of a Capital A. (It's really just a one byte integer.)
//
//Adding a random value between 0 and 25 to the ascii value of A gives you the ascii value of a letter between A and Z.
//
//Play with it.
//
//cout<<(char)('A'+2)<<endl;
//
//prints a C on the screen.

string randStrings(int numOfChars)
{
	string result;
	for(int i=0;i<numOfChars;i++)
	{
		result+='A'+rand()%26;
	}
	return result;
}

int randInt(int lower,int upper)
{
	if(upper-lower<RAND_MAX)
	{
		return (rand()%(upper-lower))+lower;
	}
	else
	{
		int r=rand()*RAND_MAX+rand();
		return (r % (upper-lower))+lower;
	}
}


//phoneBook.h
#pragma 
#include "listing.h"
using namespace std;

class phoneBook
{

public:

	void run();
	void ADD();
	void HELP();
	void FIND();
	void REPORT();
	void generateRandItems(int num);
	

private:
	listing Listing;
};


//phoneBook.cpp

#include "phoneBook.h"
#include "misc.h"
#include <iostream>
#include <fstream>
#include <map>
using namespace std;

void phoneBook::run()
	{
	HELP();
	string command=" ";
	while((command!="QUIT")&&(command!="8"))
	{
		cout<<"Enter a command: ";
		cin>>command;
		if((command=="Add")||(command=="1"))
		{
			ADD();
		}
		else if((command=="Find")||(command=="2"))
		{
			FIND();
		}
		
		else if((command=="Report")||(command=="5"))
		{
			REPORT();
		}
		
		else if((command=="TEST")||(command=="7"))
		{
			generateRandItems(10);
			REPORT();			
			
		}
		else if((command=="QUIT")||(command=="8"))
		{
			cout <<"Goodbye"<<endl;
			
		}
		else if ((command=="HELP")||(command=="0"))
		{
			HELP();
		}
		else
		{
			cout<<"Unrecognized command."<<endl;
		}
	}
}

void phoneBook::HELP()
{
	cout<<"Phone Book Commands"<<endl;
	cout<<"----------------------"<<endl;
	cout<<" 0) HELP"<<endl;
	cout<<" 1) Add"<<endl;
	cout<<" 2) Find"<<endl;
	cout<<" 5) Report"<<endl;
	cout<<" 7) Test"<<endl;
	cout<<" 8) Quit"<<endl;
	cout<<endl;
}

void phoneBook::ADD()
{
	string n;
	string c;
	string p;
	string a;

	map<string,Person> phoneData;
	cin.ignore();
	cout << "Enter your name: ";
	getline(cin, n);

	cout<<"Enter your category: ";
	getline(cin, c);

	cout << "Enter your phone number: ";
	getline(cin, p);

	cout << "Enter your address: ";
	getline(cin, a);

	Person data(n,c,p,a);
	phoneData[p]=data;
	
}

void phoneBook::FIND()
{
	string n;
	
	map<string,Person> phoneData;
	cin.ignore();
	cout << "Enter a name to find: ";
	cin >> n;

	map<string,Person>::iterator mapiter;

	mapiter=phoneData.begin();

	while(mapiter!=phoneData.end())
	{
		cout<<(*mapiter).first<<" ---  "<<(*mapiter).second<<endl;
		mapiter++;
	}
}



void phoneBook::REPORT()
{
	cout<<Person::HeaderString()<<endl;
	for(unsigned int i=0;i<Listing.size();i++)
	{
		cout<< Listing.getPerson(i).ToString()<<endl;
	}
}

void phoneBook::generateRandItems(int num)
{
	Person gi;
	for(int i=0;i<num;i++)
	{
		gi.randomize();
		//Listing.addNewListing(string n, string c, string p, string a);
	}
}



//Person.h
//Person.h
#pragma once 
#ifndef Person_h
#define Person_h
#include <string>
using namespace std;


enum PersonIndex{byName,byCategory};




class Person
{
public:
	static PersonIndex index;
	Person(){}
	Person(string n,string c,string p,string a);
	string ToString();
	static string HeaderString();
	void randomize();
	bool operator >(const Person & rhs) const;
	bool operator ==(const Person & rhs) const;
	bool operator >=(const Person & rhs) const {return ((*this) > rhs ) || ((*this) == rhs );}
	bool operator <(const Person & rhs)  const {return !((*this) >= rhs );}
	bool operator <=(const Person & rhs) const {return !((*this) > rhs );}
	bool operator !=(const Person & rhs) const {return !((*this) == rhs );}
	friend ostream& operator << (ostream& ostr, const Person& p);
	
  
public:
	string name,category,phone,address;
};


ostream& operator << (ostream& ostr, const Person& p);



#endif



//Person.cpp
//Person.cpp
#include "Person.h"
#include "misc.h"
#include <stdlib.h>
#include <assert.h>
#include <iostream>
#include <iomanip>
using namespace std;


PersonIndex Person::index = byName;



Person::Person(string n, string c, string p, string a)
{
	name = n;
	category = c;
	phone = p;
	address = a;
}


bool Person::operator >(const Person & rhs) const
{
	if(Person::index == byName)
	{
		return name>rhs.name;
	}
	else
	{
		assert(index == byCategory);
		return category>rhs.category;
	}
}




// Changed so that BST::find is more practical

bool Person::operator ==(const Person & rhs) const
{
	return (name==rhs.name);
}



ostream& operator << (ostream& ostr, const Person& p)
{
	// a little example of C++ formated output
	ostr<<setiosflags(ios::left);
	ostr<<setw(10);
	ostr << p.name;
	ostr<<setw(22);
	ostr << p.category;
	ostr<<setw(12);
	ostr << p.phone;
	ostr<<setw(30);
	ostr << p.address;
	
	return ostr;
}

string Person::ToString()
{
	string result="";
	result+=(name,' ',15);
	result+=(category, ' ' , 15);
	result+=(phone, ' ' , 15);
	result+=(address, ' ' , 15);

	return result;
}


string Person::HeaderString()
{
	char fill='-';
	string result="";
	result+=padRight("Name",fill,15);
	result+=padLeft("Category",fill,10);
	result+=padLeft("Phone Number",fill,8);
	result+=padLeft("Address",fill,8);
	
	return result;
}

void Person::randomize()
{	
	 name=randStrings(5);
	 category=randStrings(15);
	 phone=randInt(7,10);
	 address=randStrings(255);
}
[/code

[code]
//main.cpp
#include <iostream>
#include <string>
#include "phoneBook.h"

int main()
{
	phoneBook myphoneBook;
	myphoneBook.run();
	return 0;
}



This post has been edited by JackOfAllTrades: 19 November 2011 - 04:56 AM
Reason for edit:: Split into the individual files the code represents


Is This A Good Question/Topic? 0
  • +

Replies To: STL Map problems

#2 jimblumberg   User is online

  • member icon

Reputation: 5789
  • View blog
  • Posts: 17,700
  • Joined: 25-December 09

Re: STL Map problems

Posted 18 November 2011 - 11:14 PM

Part of your problem may be in this function:
string Person::ToString()
{
	string result="";
	result+=(name,' ',15);
	result+=(category, ' ' , 15);
	result+=(phone, ' ' , 15);
	result+=(address, ' ' , 15);

	return result;
}

I get these warnings:

Quote

main.cpp||In member function ‘std::string Person::ToString()’:|
main.cpp|445|warning: left-hand operand of comma has no effect|
main.cpp|445|warning: right-hand operand of comma has no effect|
main.cpp|446|warning: left-hand operand of comma has no effect|
main.cpp|446|warning: right-hand operand of comma has no effect|
main.cpp|447|warning: left-hand operand of comma has no effect|
main.cpp|447|warning: right-hand operand of comma has no effect|
main.cpp|448|warning: left-hand operand of comma has no effect|
main.cpp|448|warning: right-hand operand of comma has no effect|
Not really sure what exactly you are trying to accomplish here but this code it probably not doing what you expect. You may want to try printing out result after each addition so you can see what is actually happening.

I would suggest that you concentrate on your add and display routines. If you can not add something you probably won't be able to do much else. If you can't display your data properly how will you know what is happening.

I would start by taking one class at a time and insure that this class works properly before you start putting all the classes together. You have at least a couple of add type routines so I'm not sure which one you don't think is working correctly.

Start at your "base class" which is probably your Person class. Get this class to work correctly before you try to use it in your other classes. You should not have member functions that are only really required for testing the class. These functions should be external to your classes. An example:
void Person::randomize()
{
	 name=randStrings(5);
	 category=randStrings(15);
	 phone=randInt(7,10);
	 address=randStrings(255);
}


What purpose would this function have after testing is completed?

For your Person class you probably should provide a way of changing the individual member variables, and these member variables should probably be private.


Jim
Was This Post Helpful? 1
  • +
  • -

#3 needhelp101   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 60
  • Joined: 18-November 11

Re: STL Map problems

Posted 18 November 2011 - 11:30 PM

I did not get that errors that you are getting. In fact, I am able to compile my code. I'm using Microsoft Visual C++ 2010 Express to compile my code, and it compiles with no problem. So, I'm not sure why yours is getting an error, and mine isn't. But that is just for when the user types in the command for FIND, REPORT, or TEST. It's just how the data is to be laid out.


The randomize is for the TEST command. The TEST command is supposed to generate 10 random items that have a name, category, phone number, and address.

The one I am trying to work on is my add function. The information that i enter in, doesn't seem to get stored in the phone book. The coding for the is under the phoneBook.cpp part. That line starts with void phoneBook::ADD()
Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6259
  • View blog
  • Posts: 24,028
  • Joined: 23-August 08

Re: STL Map problems

Posted 19 November 2011 - 05:04 AM

I can only assume that you were given, either through copy and paste or from your instructor, the large part of this code. Go back to your book and/or notes and read up on classes. Don't skim the material, read it thoroughly and try to understand it.

Your listing class contains a vector of Person objects, which is a collection, your phoneBook class contains an instance of the listing class. Think about that.
Was This Post Helpful? 1
  • +
  • -

#5 jimblumberg   User is online

  • member icon

Reputation: 5789
  • View blog
  • Posts: 17,700
  • Joined: 25-December 09

Re: STL Map problems

Posted 19 November 2011 - 06:03 AM

Quote

I did not get that errors that you are getting. In fact, I am able to compile my code. I'm using Microsoft Visual C++ 2010 Express to compile my code, and it compiles with no problem.

You need to check your compiler settings and insure it is set to generate warnings, and set to maximum warnings. Visual C++ does not, by default, generate any warnings. The program also compiled on my machine, with the warnings, but just because a program compiles does not mean the program is correct.

Quote

The randomize is for the TEST command. The TEST command is supposed to generate 10 random items that have a name, category, phone number, and address.

My point about this randomize function is that it should not be part of your class. It should be external because when you have finished testing and debugging your program this "TEST" command will no longer be needed.

Jim
Was This Post Helpful? 0
  • +
  • -

#6 needhelp101   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 60
  • Joined: 18-November 11

Re: STL Map problems

Posted 19 November 2011 - 06:55 AM

Well, right now i'm gonna forget about the test function for right now. I'm just going to work on one function at a time. I'm gonna start off with my ADD function. My ADD function does not seem to be storing the input from the user into the phone book. Can you help me to figure out why the input from the user is not storing?
Was This Post Helpful? 0
  • +
  • -

#7 jimblumberg   User is online

  • member icon

Reputation: 5789
  • View blog
  • Posts: 17,700
  • Joined: 25-December 09

Re: STL Map problems

Posted 19 November 2011 - 07:00 AM

As I said previously you need to show me which class you are actually talking about. Tell me the complete name of the function, and the class name to which it belongs.

Jim
Was This Post Helpful? 0
  • +
  • -

#8 needhelp101   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 60
  • Joined: 18-November 11

Re: STL Map problems

Posted 19 November 2011 - 07:08 AM

The ADD function is under my phoneBook.cpp file. The full function name is void phoneBook::ADD()
Was This Post Helpful? 0
  • +
  • -

#9 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6259
  • View blog
  • Posts: 24,028
  • Joined: 23-August 08

Re: STL Map problems

Posted 19 November 2011 - 07:15 AM

Did you read what I wrote above, and make ANY ATTEMPT at what I told you? Looks like you were apparently told something similar elsewhere.

Posting to multiple forums in an attempt to reduce your responsibility in the learning process is generally frowned upon. You are obviously supposed to be learning about classes and composition. So make a real attempt at grasping the whole class/object-oriented paradigm. I've given you hints in the post above.
Was This Post Helpful? 1
  • +
  • -

#10 needhelp101   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 60
  • Joined: 18-November 11

Re: STL Map problems

Posted 19 November 2011 - 07:20 AM

I'm not trying to reduce my responsibility. I'm going out to places to seek some more help. But in any case, I have been working on my program, trying to implement the hints you gave me in your other post, but things are not working out.
Was This Post Helpful? 0
  • +
  • -

#11 jimblumberg   User is online

  • member icon

Reputation: 5789
  • View blog
  • Posts: 17,700
  • Joined: 25-December 09

Re: STL Map problems

Posted 19 November 2011 - 07:27 AM

So this is the class in question:

class phoneBook
{

public:

	void run();
	void ADD();
	void HELP();
	void FIND();
	void REPORT();
	void generateRandItems(int num);


private:
	listing Listing;
};



And this is the function in question:
void phoneBook::ADD()
{
	string n;
	string c;
	string p;
	string a;

	map<string,Person> phoneData;
	cin.ignore();
	cout << "Enter your name: ";
	getline(cin, n);

	cout<<"Enter your category: ";
	getline(cin, c);

	cout << "Enter your phone number: ";
	getline(cin, p);

	cout << "Enter your address: ";
	getline(cin, a);

	Person data(n,c,p,a);
	phoneData[p]=data;
	
}


What is the purpose of the last two calls? Where is phoneData defined? I don't see it in the phoneBook class class, nor do I see it in the listing class.

Jim
Was This Post Helpful? 0
  • +
  • -

#12 needhelp101   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 60
  • Joined: 18-November 11

Re: STL Map problems

Posted 19 November 2011 - 07:35 AM

phoneData is in the line map<string,Person> phoneData;

I was working on this with a friend, and he used those last two lines to store the input from the user into the phone book. And it worked for him.
Was This Post Helpful? 0
  • +
  • -

#13 needhelp101   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 60
  • Joined: 18-November 11

Re: STL Map problems

Posted 19 November 2011 - 08:11 AM

Unless there is another way of storing the data into the phone book that i'm not thinking of.I thought this would've stored the input from the user, but I guess it doesn't
Was This Post Helpful? 0
  • +
  • -

#14 jimblumberg   User is online

  • member icon

Reputation: 5789
  • View blog
  • Posts: 17,700
  • Joined: 25-December 09

Re: STL Map problems

Posted 19 November 2011 - 08:11 AM

Those two lines are not actually doing anything. When this function ends the data held in both the data and phoneData variables will be destroyed because they are local to this function.

Quote

I was working on this with a friend, and he used those last two lines to store the input from the user into the phone book. And it worked for him.


It looks like your code is quite different from your friends.

I recommend that you throw away everything you have except your Person class. Change this classes data variables to private. Work at getting this class working properly by it's self. Add public data members to allow you to get/set the private data. Once you have this class functioning properly you can use it as a base of a phone book. And as stated by other posters in both forums you need to go back and "hit the books".

Posting on multiple boards will usually cause many people to stop helping. I dislike when people are posting to different boards because it makes it much harder to judge the actual level of competence of the person asking the question.


Jim

This post has been edited by jimblumberg: 19 November 2011 - 08:13 AM

Was This Post Helpful? 0
  • +
  • -

#15 needhelp101   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 60
  • Joined: 18-November 11

Re: STL Map problems

Posted 19 November 2011 - 08:20 AM

ok, i'll try that. but, there has to be a better way of storing the input from the user in the ADD function. could you point me in the right direction of doing so
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3