Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Help with STL container list for INformation Manager

  1. Apr 15, 2009 #1
    Help with STL container list for INformation Manager!!!!

    I'm trying to create a info manager like rolodex.
    Rolodex.cpp
    List() function
    Code (Text):

    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 (Text):
    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?
     
  2. jcsd
  3. Apr 15, 2009 #2

    Hurkyl

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

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

    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>
     
  4. Apr 15, 2009 #3
    Re: Help with STL container list for INformation Manager!!!!

    I've got to create a rolodex with a container for cards which contains information.
    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
     
  5. Apr 15, 2009 #4
    Re: Help with STL container list for INformation Manager!!!!

    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?
     
  6. Apr 15, 2009 #5
    Re: Help with STL container list for INformation Manager!!!!

    I tried to create a new object to call the Add function but the compiler said that there is no default constructor available
    Code (Text):
    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 (Text):
    int main ()
    {  
    Rolodex Info;
    Rolocard ncard;
    Info.Add(ncard);

        return 0;
    }
     
  7. Apr 15, 2009 #6
    Re: Help with STL container list for INformation Manager!!!!

    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.
     
  8. Apr 15, 2009 #7

    Hurkyl

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

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

    And what did you do about that?
     
  9. Apr 15, 2009 #8
    Re: Help with STL container list for INformation Manager!!!!

    i did that but when i try to print the Rolocard that i added to the list<Rolocard>scard, it doesn't print anything.
     
  10. Apr 15, 2009 #9
    Re: Help with STL container list for INformation Manager!!!!

    Code (Text):

    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 (Text):

    cin >> first;
    card.setFirst(first); // assuming setFirst exists
     
     
    Last edited: Apr 15, 2009
  11. Apr 22, 2009 #10
    Re: Help with STL container list for INformation Manager!!!!

    No there's no setfirst function. I used member init. so there was no reason to create set functions.
     
  12. Apr 22, 2009 #11
    Re: Help with STL container list for INformation Manager!!!!

    Great. That's not really the point though. The problem is that that a statement like
    Code (Text):
    cin >> card.getFirst();
    isn't going to work. Actually, the method probably should be written something like this:
    Code (Text):

    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: Apr 23, 2009
  13. Apr 23, 2009 #12
    Re: Help with STL container list for INformation Manager!!!!

    I know, my problem is that "cin" doesn't accept whitespace. for example, if i enter the address, the input stream terminates.
     
  14. Apr 23, 2009 #13

    Hurkyl

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

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

    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....
     
  15. Apr 23, 2009 #14
    Re: Help with STL container list for INformation Manager!!!!

    To get the address including the spaces you would use http://www.cppreference.com/wiki/string/getline" [Broken] as follows:
    Code (Text):

    getline(cin, add);
     
     
    Last edited by a moderator: May 4, 2017
  16. Apr 23, 2009 #15
    Re: Help with STL container list for INformation Manager!!!!

    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
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook