Help with STL container list for INformation Manager

  • Thread starter ghost305
  • Start date
  • #1
14
0

Main Question or Discussion Point

Help with STL container list for INformation Manager!!!!

I'm trying to create a info manager like rolodex.
Rolodex.cpp
List() function
Code:
void Rolodex::List()
{
	list<string>::iterator cIterator;
   for (cIterator = cardlist.begin(); cIterator != cardlist.end();    ++cIterator)
   {
      cout << *cIterator << endl; //outputs its contents
   }
}//end List Function
Add() function
Code:
void Rolodex::Add(list<Rolocard>& cardlist)
{

	string first, last, add, occ, phone;
	
	cout << "Creating new Card "<< endl;
	cout << "----------------------"<<endl;
	cout << "Enter First Name: " << endl;
	cin >> first;
	cout << "Enter Last Name: "<< endl;
	cin >> last;
	cout << "Enter Address: "<< endl;
	cin >> add;
	cout << "Enter Occupation: " << endl;
	cin >> occ;
	cout << "Enter Phone Number: " << endl;
	cin >> phone;
	Rolocard newcard(first, last, add, occ, phone);	
	cardlist.push_back(newcard);

	
}
private member in rolodex class:
list <Rolocard> sCard;

---------------------------------------------------------
what is the issue with this code?
 

Answers and Replies

  • #2
Hurkyl
Staff Emeritus
Science Advisor
Gold Member
14,916
17


what is the issue with this code?
I don't know. What is it supposed to do? And what do you actually observe it doing?



I don't know if this is actually a problem, but I see that elsewhere in your code, you have used the name cardlist to refer to a std::list<Rolocard>. But it's not obvious that there is any such variable visible to the List function -- and if there were one of type std::list<Rolocard>, it would be an error to access it as a std::list<std::string>
 
  • #3
14
0


I've got to create a rolodex with a container for cards which contains information.
* list - displays all the cards in the rolodex, in alphabetical order from beginning to end.
* view - displays the card at the current position.
* flip - updates the current position to the next card, and displays it. Flipping past the last card wraps around to the first card.
* search - finds and displays a card, and makes it the current position in the rolodex. This command prompts for the last name for searching. If a matching card is found, it is displayed and is set as the current position. If no matching card is found, the card that immediately follows the lookup name is displayed and set as the current position (e.g. if "H" is entered as the last name, the first card with a last name following "H" in alphabetical order is displayed). If there is no following card, displays "Couldn't find: name"
* add - adds a new card to the rolodex. Prompts for each field value, reads it, and enters the new card in the correct position in the rolodex (based on alphabetical order). The new card is set as the current position.
* remove - removes the card at the current position. It displays the card and prompts for a confirmation for removal. The following card is set as the current position.
* modify - updates the card at the current position. Enters a mode that requests the field to be updated (e.g. phone #), displays the existing value and prompts for the new value, reads it, and updates the card. Continues prompting for additional changes until all changes are done (e.g. a 0 is entered for the field # to change). If the last name is changed, the card must be moved to the correct position in the rolodex (and is set as the current position).
* quit - exits the program.
I've already create the rolocard class which holds the information. Now my next step is to create the rolodex class which will manage the information. I can't seem to use the container private member in other member functions
 
  • #4
560
1


I can't seem to use the container private member in other member functions
What error do you get when you try?

And can be more specific. Which private member can't you use (sCard?) and which member function are you unable to use it in?
 
  • #5
14
0


I tried to create a new object to call the Add function but the compiler said that there is no default constructor available
Code:
void Rolodex::Add(Rolocard card)
{

	string first, last, add, occ, phone;
	
	cout << "Creating new Card "<< endl;
	cout << "----------------------"<<endl;
	cout << "Enter First Name: " << endl;
	cin >> card.getFirst();
	cout << "Enter Last Name: "<< endl;
	cin >> card.getLast();
	cout << "Enter Address: "<< endl;
	cin >> card.getAdd();
	cout << "Enter Occupation: " << endl;
	cin >> card.getOcc();
	cout << "Enter Phone Number: " << endl;
	cin >> card.getPhone();
	Rolocard NewCard = Rolocard (card.getFirst(), card.getLast(), card.getAdd(), card.getOcc(), card.getPhone());
	sCard.push_back(NewCard);

	
}
I called it in main like this:
Code:
int main ()
{	
Rolodex Info;
Rolocard ncard;
Info.Add(ncard);

	return 0;
}
 
  • #6
560
1


Is there a default constructor available for both Rolodex and Rolocard? (Default constructor meaning like, Rolodex().)

Sometimes the STL classes will get very upset if the answer is no, because they use the default constructor internally. The STL will probably often also expect a copy constructor.

C++ will put in a default (and I think also a copy) constructor by default, but if you write at least one constructor yourself for a class then these "invisible" constructors go away.
 
  • #7
Hurkyl
Staff Emeritus
Science Advisor
Gold Member
14,916
17


I tried to create a new object to call the Add function but the compiler said that there is no default constructor available
And what did you do about that?
 
  • #8
14
0


Is there a default constructor available for both Rolodex and Rolocard? (Default constructor meaning like, Rolodex().)

Sometimes the STL classes will get very upset if the answer is no, because they use the default constructor internally. The STL will probably often also expect a copy constructor.

C++ will put in a default (and I think also a copy) constructor by default, but if you write at least one constructor yourself for a class then these "invisible" constructors go away.
i did that but when i try to print the Rolocard that i added to the list<Rolocard>scard, it doesn't print anything.
 
  • #9
9
0


Code:
void Rolodex::Add(Rolocard card)
{

	string first, last, add, occ, phone;
	
	cout << "Creating new Card "<< endl;
	cout << "----------------------"<<endl;
	cout << "Enter First Name: " << endl;
	cin >> card.getFirst(); //problem
	cout << "Enter Last Name: "<< endl;
	cin >> card.getLast(); //problem       
	cout << "Enter Address: "<< endl;
	cin >> card.getAdd(); //problem
	cout << "Enter Occupation: " << endl;
	cin >> card.getOcc(); //problem
	cout << "Enter Phone Number: " << endl;
	cin >> card.getPhone(); //problem

        //probably unecessary
	Rolocard NewCard = Rolocard (card.getFirst(), card.getLast(),
                                    card.getAdd(), card.getOcc(), card.getPhone());
	sCard.push_back(NewCard);
}
The lines marked "problem" above definitely need to be changed, unless your get methods do something really weird. You probably want something more like
Code:
cin >> first;
card.setFirst(first); // assuming setFirst exists
 
Last edited:
  • #10
14
0


Code:
void Rolodex::Add(Rolocard card)
{

	string first, last, add, occ, phone;
	
	cout << "Creating new Card "<< endl;
	cout << "----------------------"<<endl;
	cout << "Enter First Name: " << endl;
	cin >> card.getFirst(); //problem
	cout << "Enter Last Name: "<< endl;
	cin >> card.getLast(); //problem       
	cout << "Enter Address: "<< endl;
	cin >> card.getAdd(); //problem
	cout << "Enter Occupation: " << endl;
	cin >> card.getOcc(); //problem
	cout << "Enter Phone Number: " << endl;
	cin >> card.getPhone(); //problem

        //probably unecessary
	Rolocard NewCard = Rolocard (card.getFirst(), card.getLast(),
                                    card.getAdd(), card.getOcc(), card.getPhone());
	sCard.push_back(NewCard);
}
The lines marked "problem" above definitely need to be changed, unless your get methods do something really weird. You probably want something more like
Code:
cin >> first;
card.setFirst(first); // assuming setFirst exists
No there's no setfirst function. I used member init. so there was no reason to create set functions.
 
  • #11
9
0


No there's no setfirst function.
Great. That's not really the point though. The problem is that that a statement like
Code:
cin >> card.getFirst();
isn't going to work. Actually, the method probably should be written something like this:
Code:
void Rolodex::Add()
{
	string first, last, add, occ, phone;
	
	cout << "Creating new Card "<< endl;
	cout << "----------------------"<<endl;
	cout << "Enter First Name: " << endl;
	cin >> first;
	cout << "Enter Last Name: "<< endl;
	cin >> last;      
	cout << "Enter Address: "<< endl;
	cin >> add;
	cout << "Enter Occupation: " << endl;
	cin >> occ;
	cout << "Enter Phone Number: " << endl;
	cin >> phone;

	Rolocard NewCard = Rolocard(first, last, add, occ, phone);
	sCard.push_back(NewCard);
}
 
Last edited:
  • #12
14
0


Great. That's not really the point though. The problem is that that a statement like
Code:
cin >> card.getFirst();
isn't going to work. Actually, the method probably should to be written something like this:
Code:
void Rolodex::Add()
{
	string first, last, add, occ, phone;
	
	cout << "Creating new Card "<< endl;
	cout << "----------------------"<<endl;
	cout << "Enter First Name: " << endl;
	cin >> first;
	cout << "Enter Last Name: "<< endl;
	cin >> last;      
	cout << "Enter Address: "<< endl;
	cin >> add;
	cout << "Enter Occupation: " << endl;
	cin >> occ;
	cout << "Enter Phone Number: " << endl;
	cin >> phone;

	Rolocard NewCard = Rolocard(first, last, add, occ, phone);
	sCard.push_back(NewCard);
}
I know, my problem is that "cin" doesn't accept whitespace. for example, if i enter the address, the input stream terminates.
 
  • #13
Hurkyl
Staff Emeritus
Science Advisor
Gold Member
14,916
17


I know, my problem is that "cin" doesn't accept whitespace.
You mean >>, not cin. And it's not that it "doesn't accept"; it's that when using it to extract a string, it's designed to use whitespace as delimiters.

And that's a good thing. After all, you're relying on that behavior to be able to extract, say, the first name as a string, rather than all of the input thrust together into first....
 
  • #14
9
0


To get the address including the spaces you would use http://www.cppreference.com/wiki/string/getline" [Broken] as follows:
Code:
getline(cin, add);
 
Last edited by a moderator:
  • #15
14
0


do i access the current position in a list container like this?
std::list < string >::iterator iter = rolodex.begin();
do i access next as:
iter + 1
 

Related Threads for: Help with STL container list for INformation Manager

  • Last Post
Replies
1
Views
2K
Replies
1
Views
3K
  • Last Post
Replies
15
Views
578
Replies
12
Views
783
Replies
4
Views
2K
  • Last Post
Replies
3
Views
1K
Replies
2
Views
5K
Top