Thread: returning a string value???

  1. #1
    Hmmm.
    Join Date
    Feb 2005
    Location
    Washington.
    Posts
    5

    Question how do you return the value of a string???

    Hi all. I want to know how to return the value of a string...when I try to do that, it gives me an error about converting a char * to an int.
    So, how do I return the value of a string?
    I have tried:
    Code:
    return stringname;
    return *stringname;
    I have to go for awhile, so don't expect any immediate replies from me. Thanks in advance.
    Edit:
    To be more specific, I am returning the value of a string that the user typed in, for example:
    Code:
    class littleCrab {
    public:
    	littleCrab::whatcolor() {
    		cout<<"What color is your crab?"<<endl;
    			cin>>color;
    		return *color;
    //^^^^^^^^^^^^^^^
    
    	}
    	littleCrab::howmanyeyes() {
    		cout<<"How many eyes does your crab have?"<<endl;
    		cin>>eyes;
    		cout<<"Your crab has "<<eyes<<endl;
    		return eyes;
    	}
    private:
    	char *color;
    	int eyes;
    	int legs;
    };
    Awe, come'on! Someone must know!
    Last edited by mero42; 04-03-2005 at 05:57 PM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Well, "color" doesn't have any space allocated to it, so "cin >> color" will likely just crash your program. However, to return a character pointer correctly, you just return the name of the variable itself. (Assuming it actually has something allocated for it.)

    Why don't you just use the string class instead? Otherwise, you'll need to first learn how dynamic memory allocation works first, because as stated, you don't have anything allocated for your "string".

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Hmmm.
    Join Date
    Feb 2005
    Location
    Washington.
    Posts
    5
    Thank you.
    I changed char * to string, but I still get an error that says: "cannot convert from std::string to int." Why would that happen?

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Probalbly because it can't convert a 'std::string' to an 'int'.

    Oh, I suppose that's not good enough for you. Well, how about actually giving your function the return value, instead of letting it default to an integer?

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Hmmm.
    Join Date
    Feb 2005
    Location
    Washington.
    Posts
    5
    As in doing this?
    Code:
    littleCrab::string whatcolor() {
    		cout<<"What color is your crab?"<<endl;
    			cin>>color;
    		return color;
    	}

  6. #6
    Registered User
    Join Date
    Jan 2003
    Posts
    311
    if littleCrab is a class, you would do
    Code:
    std::string littleCrab::whatcolor() {
        std::cout << "What color is your crab?\n";
        std::cin >> color;
        return color;
    }
    You can also put a typedef within littleCrab
    Code:
    class littleCrab {
    public:
    typedef std::string string;
        string whatcolor();
    };
    littleCrab::string littleCrab::whatcolor() {
    ...
    }

  7. #7
    Hmmm.
    Join Date
    Feb 2005
    Location
    Washington.
    Posts
    5
    Thanks, grib. I really appreciate it!

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You'd be better off using a name space than typedefing the string.

    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    Hmmm.
    Join Date
    Feb 2005
    Location
    Washington.
    Posts
    5
    Well, using
    Code:
    std::string littleCrab::whatcolor() {
    works just fine for me at the moment.

  10. #10
    Registered User
    Join Date
    Jan 2003
    Posts
    311
    Quote Originally Posted by quzah
    You'd be better off using a name space than typedefing the string.
    Personally I like for my classes to have typedefs for much of what they work with. That makes it easer for users of the class to keep up with changes to the interface. Same reason string provides string::size_type. For the OP std::string is fine, didn't mean to imply otherwise. It's just that littleCrab::string means "the type string in the context of littleCrab", there could be a littleCrab namespace, but I assumed that littleCrab was a class.

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You misunderstood. Why don't you just use:
    Code:
    using namespace std;
    And just do:
    Code:
    class foo
    {
        ...stufff...
    };
    
    string  foo::returnsastring( )
    {
        string bar;
        ...stuff...
    
        return bar;
    }
    And like I said, skip typedef all together? I didn't say to use a namespace for your class. Use the namespace for std so you don't keep having to type "std::", AND you don't need to typedef it.

    Quzah.
    Hope is the first step on the road to disappointment.

  12. #12
    Registered User
    Join Date
    May 2003
    Posts
    82
    Wouldn't using the std namespace in a class pollute the namespace of any code the uses the class?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Please check my C++
    By csonx_p in forum C++ Programming
    Replies: 263
    Last Post: 07-24-2008, 09:20 AM
  2. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM
  3. Replies: 4
    Last Post: 03-03-2006, 02:11 AM
  4. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM