Thread: Class accessor functions returning strings?

  1. #1
    Shadow12345
    Guest

    Question Class accessor functions returning strings?

    I have been fooling around with classes lately, but so far the only variables I've been able to successfully return are integral ones, (mostly of type int and float). I am not exactly sure how to make this program work the way I want it to, and I could use a little assistance.

    //Simple class program, self explanatory. Uses inline definitions//
    #include <iostream.h>
    #include <string.h>

    class cat
    {
    public:
    char getname() {return itsname[10];}
    void setname(char name[10]) {itsname[10] = name[10];}

    int getage() {return itsage;}
    void setage(int age) {itsage = age;}

    int getheight() {return itsheight;}
    void setheight(int height) {itsheight = height;}

    private:
    int itsage;
    int itsheight;
    char itsname[10];
    };




    int main()
    {
    int age;
    int height;
    char name[10];
    cout << "\nWhat is your cat's name?";
    cin >> name;

    cout << "\nHow old is your cat?";
    cin >> age;

    cout << "\nHow tall is your cat?";
    cin >> height;

    cat one;
    one.setname(name);
    one.setage(age);
    one.setheight(height);
    cout << "\nCat one's name is " << one.getname();
    cout << "\nOne's age is " << one.getage() << " years old";
    cout << "\nOne's height is " << one.getheight() << " cm tall";
    return 0;
    }

    This program compiles fine, but when I enter the name, no matter what I put, it always comes out as a @ (yes, the 'at' symbol). It does the same thing whether or not I use #include <string.h>
    ok, thanks for your time

  2. #2
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    chars are not strings. You need to either use std::string (I recommend them), or use char*.

    Code:
    // BROKEN.. returning a single character
    char getname() {return itsname[10];}
    
    // fixed, returning whole name
    char* getname() { return itsname;}
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  3. #3
    Shadow12345
    Guest
    I tried making the change you suggested. Although the program still compiles successfully, it does not tell me the cat's name, it keeps outputting some weird symbol, not the name I enter. I think there is more I have to change than just the char* getname() {return itsname;}

    Also, I don't know how to use std::string.

    -thanks again :-)

  4. #4
    Registered User
    Join Date
    Dec 2001
    Posts
    194
    snip

    char getname() {return itsname[10];}
    void setname(char name[10]) {itsname[10] = name[10];}

    Both of those functions are wrong. When using char * or char[] for strings you must use the strcpy function to copy them

    void setname(char * name) { strpy(itsname,name); }
    char getname() { return itsname;}

    You were getting weird symbols cus the string were never filled in
    Look into using <string> and <iostream>
    the string class is very easy to use
    ex
    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main()
    {
       string str1,str2,str3;
      str1 = "Hi there";
      str2 = str1 + ", whats your name";
      str3 = "The string class has alot of operators overloaded";
     cout << str1 << endl << str2 << endl << str3 << endl;
     return 0;
    }
    Here is a reference http://www.cppreference.com/cppstring.html

  5. #5
    Shadow12345
    Guest

    Cool Thanx!!!!

    Hey thanks a lot!! That is very useful information!!

  6. #6
    Shadow12345
    Guest

    Question Using Namespace std;

    Exactly why did you include the 'using namespace std' in your code? Doesn't it work without it? I have never seen that before, so i am a little bit confused.

    I have gotten a little bit better though, so thanks again.

  7. #7
    Registered User
    Join Date
    Aug 2001
    Posts
    403

    std

    using namespace std; allows you to leave off the std:: part of std::cout, and yes some compilers allow the old method of using headers that end in .h and not including std:: but it should not be relied on as the C++0x revision is coming near and may completely wipe out the global namespace which would call for major modifications of all code that is currently improper but working.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. returning class and struct members
    By simone.marras in forum C++ Programming
    Replies: 17
    Last Post: 03-16-2009, 11:10 AM
  2. Sensibility of returning a 2D array from a Class
    By goatslayer in forum C++ Programming
    Replies: 9
    Last Post: 09-08-2007, 07:23 PM
  3. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  4. Need help to build network class
    By weeb0 in forum C++ Programming
    Replies: 0
    Last Post: 02-01-2006, 11:33 AM
  5. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM