Help with STL container list for INformation Manager

In summary: I don't know what a default constructor is. Can you elaborate on what "a default constructor is" and what it does?A default constructor is a constructor that is called when no other constructor is found for a class. It is usually just a simple copy constructor.
  • #1
ghost305
14
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
  • #2


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>
 
  • #3


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


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?
 
  • #5


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


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


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?
 
  • #8


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.
 
  • #9


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
 

1. What is an STL container list?

An STL container list is a data structure in the Standard Template Library (STL) that stores a collection of elements in a linear sequence. Each element is stored in a specific order and can be accessed individually using iterators.

2. How do I use an STL container list?

To use an STL container list, you must include the <list> header in your code. Then, you can declare a list using the syntax std::list<datatype> listname;. You can then add elements to the list using the push_back() or push_front() functions, and access and modify elements using iterators.

3. What is the advantage of using an STL container list?

One advantage of using an STL container list is that it allows for fast insertion and deletion of elements at any position in the list, compared to other linear data structures such as arrays. Additionally, it automatically manages the memory allocation and deallocation of elements, making it easier to use and less prone to errors.

4. Can I sort an STL container list?

Yes, you can sort an STL container list using the sort() function. This function uses the <algorithm> header and takes in two parameters - the first being the beginning iterator of the list and the second being the end iterator. It will then sort the elements in ascending order based on the datatype.

5. How do I remove elements from an STL container list?

To remove elements from an STL container list, you can use the remove() or erase() functions. The remove() function will remove all elements in the list that match a specified value, while the erase() function will remove a specific element at a given position. Both functions take in iterators as parameters.

Similar threads

Replies
10
Views
958
  • Programming and Computer Science
2
Replies
66
Views
4K
  • Programming and Computer Science
Replies
3
Views
3K
  • Programming and Computer Science
Replies
6
Views
1K
  • Programming and Computer Science
Replies
1
Views
6K
  • Programming and Computer Science
3
Replies
75
Views
4K
  • Programming and Computer Science
Replies
1
Views
3K
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
5
Views
882
Back
Top