Help with STL container list for INformation Manager

AI Thread Summary
The discussion centers around creating a Rolodex information manager using STL containers in C++. The main issue highlighted is the inability to access private member variables, specifically the list of Rolocard objects, within member functions. Participants noted that the Add function should not attempt to use getter methods to set values, as this leads to issues; instead, direct input should be captured into local variables before creating a new Rolocard. Additionally, the use of `std::getline` is recommended for capturing strings with whitespace, such as addresses. The conversation emphasizes the importance of ensuring default constructors are available for both the Rolodex and Rolocard classes to avoid compilation errors.
ghost305
Messages
14
Reaction score
0
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?
 
Technology news on Phys.org


ghost305 said:
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>
 


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
 


ghost305 said:
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?
 


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;
}
 


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.
 


ghost305 said:
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?
 


Coin said:
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.
 


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


Natr0n said:
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


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


Natr0n said:
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


ghost305 said:
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


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


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
 
Back
Top