Small Class Example

This is a discussion on Small Class Example within the C++ Programming forums, part of the General Programming Boards category; Does anyone know why this will not display the name to the console? If I call A.getName(); in the addContact ...

  1. #1
    Registered User
    Join Date
    Nov 2012
    Location
    Louisville, KY
    Posts
    12

    Question Small Class Example

    Does anyone know why this will not display the name to the console? If I call A.getName(); in the addContact function itself it will retrieve the name correctly, however I believe it deletes itself after the function addContact ends. Can anyone spot where I went wrong?

    Code:
    #include <iostream>
    
    using namespace std;
    
    class Phonebook {
        private:
        string Name;
        public:
        void menu();
        void setName(string);
        string getName(){return Name;}
        void addContact(Phonebook);
        void printContacts(Phonebook);
    };
    
    void Phonebook::menu(){
        cout << "Phonebook Menu" << endl;
        cout << "(1) Add Contact" << endl;
        cout << "Choose an option: ";
    }
    
    void Phonebook::setName(string s){
        Name = s;
    }
    
    void Phonebook::addContact(Phonebook A){
        string s;
        cout << "\n\n[Add Contact]" << endl;
        cout << "Name: ";
        cin >> s;
        A.setName(s);
    }
    
    void Phonebook::printContacts(Phonebook A){
        cout << "\n\n[Contact List]" << endl;
        cout << A.getName() << endl;
    }
    
    int main(){
        Phonebook A;
        int option = 0;
    
        A.menu();
    
        cin >> option;
    
        switch(option){
            case 1:
            A.addContact(A);
            A.printContacts(A);
            break;
        }
    
        return 0;
    }
    Edit: I was able to make it print the name correctly, I still have a question though. How can I make it store more than one name?

    New Code:

    Code:
    #include <iostream>
    
    using namespace std;
    
    class Phonebook {
        private:
        string Name;
        public:
        void menu();
        void setName(string);
        string getName(){return Name;}
        void addContact(Phonebook *);
        void printContacts(Phonebook);
    };
    
    void Phonebook::menu(){
        cout << "Phonebook Menu" << endl;
        cout << "(1) Add Contact" << endl;
        cout << "Choose an option: ";
    }
    
    void Phonebook::setName(string s){
        Name = s;
    }
    
    void Phonebook::addContact(Phonebook * A){
        string s;
        cout << "\n\n[Add Contact]" << endl;
        cout << "Name: ";
        cin >> s;
        A->setName(s);
    }
    
    void Phonebook::printContacts(Phonebook A){
        cout << "\n\n[Contact List]" << endl;
        cout << A.getName() << endl;
    }
    
    int main(){
        Phonebook A;
        int option = 0;
    
        A.menu();
    
        cin >> option;
    
        switch(option){
            case 1:
            A.addContact(&A);
            A.printContacts(A);
            break;
        }
    
        return 0;
    }
    Last edited by Khaltazar; 11-27-2012 at 03:32 PM.

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,675
    Code:
    string Name;
    this is enough for one string

    This is enough for ten strings
    Code:
    string arrayNames[10];

  3. #3
    Registered User
    Join Date
    Nov 2012
    Location
    Louisville, KY
    Posts
    12

    Post

    When I try to convert the

    Code:
    string Name;
    to

    Code:
    string Name[10];
    I get the following error

    Line: 11
    Code:
    error: could not convert '(std::string*)(&((Phonebook*)this)->Phonebook::Name)' from 'std::string* {aka std::basic_string<char>*}' to 'std::string {aka std::basic_string<char>}'

  4. #4
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,675
    This will work (general example)
    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main() {
    
        string Name[10];
    
        Name[0] = "Samaras";
    
        cout<<Name[0]<<endl;
    
        return 0;
    
    
    }
    Where do you get the error?

    I am guessing at a line of code that you treat Name as it was before (just a string), like you do here
    Code:
    void Phonebook::setName(string s){
        Name = s;
    }
    You should treat Name as an array, because this is what Name is. An array of strings .

  5. #5
    Registered User bobknows's Avatar
    Join Date
    Jan 2011
    Location
    SD
    Posts
    87
    Every time you make a call to an array you have to include a [] with the number of the member you want - 1, starting with 0. For example, if I wanted the first member of name[10] I would have to write name[0], if I want the second it is name[1], name[2] for the third, and so on up to name[9].

    edit: ninja'd

  6. #6
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,798
    You should not be passing in a Phonebook object (or pointer to one) in your member functions. Instead of:
    Code:
        switch(option){
            case 1:
            A.addContact(A);
            A.printContacts(A);
            break;
        }
    It should be:
    Code:
        switch(option){
            case 1:
            A.addContact();
            A.printContacts();
            break;
        }
    And the individual member functions should be changed to accommodate this revision.




    From a design standpoint, a Phone Book contains data on individuals (name, phone number, address, etc...). This would suggest that you have a "person" object which stores the name, phone number... and then your Phonebook class stores an array (or more usefully a std::vector) of such person objects:
    Code:
    class Person
    {
        std::string firstname;
        std::string lastname;
        std::string phone_number;
        ...
    };
    
    class Phonebook
    {
        std::vector<Person> entries;
        ...
    };
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 08-28-2012, 06:11 AM
  2. Replies: 7
    Last Post: 05-26-2005, 10:48 AM
  3. a small help here
    By ssharish in forum C Programming
    Replies: 5
    Last Post: 01-29-2005, 02:03 PM
  4. Template <class T1, class T2, class T3> error LNK2019
    By JonAntoine in forum C++ Programming
    Replies: 9
    Last Post: 10-11-2004, 12:25 PM
  5. A small problem with a small program
    By Wetling in forum C Programming
    Replies: 7
    Last Post: 03-25-2002, 08:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21