Thread: Returning Strings

  1. #1
    Unregistered User Overload's Avatar
    Join Date
    Jul 2004
    Posts
    12

    Returning Strings

    How do I return a string from a function? The only way I know how to use strings are character arrays, but how would you declare a function to return a character array?
    ._«:¤©•®™ª°*¯*°ª™®•©¤:»_. C++ ._«:¤©•®™ª°*¯*°ª™®•©¤:»_.

  2. #2
    Useless Apprentice ryan_germain's Avatar
    Join Date
    Jun 2004
    Posts
    76
    yuo return a pointer to a character array or include
    Code:
    #include<string>
    in your code and you may use "string" as a type. e.g. string myString

  3. #3
    Unregistered User Overload's Avatar
    Join Date
    Jul 2004
    Posts
    12
    1. do I need to include any header files for string types?
    2. is this considered okay in context to the information you just gave me:

    Code:
    string convert_enemy(int enemy)
    {
         switch(enemy)
         {
         case 1: return "Rabid Wolf";
                break;
         case 2: return "Bear";
                break;
         etc.
         }
    }
    ._«:¤©•®™ª°*¯*°ª™®•©¤:»_. C++ ._«:¤©•®™ª°*¯*°ª™®•©¤:»_.

  4. #4
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    just to show you what ryan is talking about
    Code:
    #include <iostream>
    #include <string>
    using std::string;
    using std::cout;
    using std::cin;
    string creturn()
    {
      string letters="hello";
      return letters;
    }
    
    int main()
    {
      string print=creturn();
      cout<<print;
      cin.get()
      return 0;
    } 
    //btw: isn't the string class nice :)
    Woop?

  5. #5
    Registered User
    Join Date
    Jul 2004
    Posts
    18
    Omg! Is it legal? I mean.. is it standart?

  6. #6
    meow nbk's Avatar
    Join Date
    Jul 2004
    Posts
    45
    Yeah, it is

    (at least.. there isn't anything nonstandard that I see with it)

  7. #7
    Registered User
    Join Date
    Jul 2004
    Posts
    18
    Really? Then please explain me why there is no that method in most books? And why most tutorials in internet using character arrays instead of this so-handy method?

  8. #8
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    umm i don't know the string class is really easy to use and its completly standard. Most tutorials are a bit out of date still very usefull but a little old
    Woop?

  9. #9
    uninteresting
    Join Date
    Jun 2002
    Posts
    66
    Because you're reading about C, not C++ possibly?

    Oh, and Overload, I think that you'll need to return like this:

    Code:
    #include <string>
    
    using namespace std; // for our purposes, you can just make this 'using std::string;', otherwise you'll have to use std::string all the time
    
    string convert_enemy(int enemy)
    {
         switch(enemy)
         {
         case 1: return string ("Rabid Wolf");
                break;
         case 2: return string ("Bear");
                break;
         etc.
         }
    }
    //EDIT: Holy sheet is it just me, or is my user info column messed up?
    Last edited by Tman; 07-29-2004 at 10:28 AM.
    *** TITANIC has quit (Excess Flood)

  10. #10
    meow nbk's Avatar
    Join Date
    Jul 2004
    Posts
    45
    I may have been wrong. But a lot of books specify you can make functions with any return type. I'll let someone else answer the rest

  11. #11
    Registered User
    Join Date
    Jul 2004
    Posts
    18
    Ok, its clear.

    Holy sheet is it just me, or is my user info column messed up?
    Its not you or your column. Its forum bug :\ I have the same in previous post.
    Last edited by rockdj; 07-29-2004 at 10:34 AM.

  12. #12
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    Its not your user collum you just posted a really long comment so the thing is adjusting for it
    Code:
    //I am posting a really long comment so that my user collum will be really small it is quite entertaining :)
    Woop?

  13. #13
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Quote Originally Posted by Tman
    Because you're reading about C, not C++ possibly?

    Oh, and Overload, I think that you'll need to return like this:

    Code:
    #include <string>
    
    using namespace std; // for our purposes, you can just make this 'using std::string;', otherwise you'll have to use std::string all the time
    
    string convert_enemy(int enemy)
    {
         switch(enemy)
         {
         case 1: return string ("Rabid Wolf");
                break;
         case 2: return string ("Bear");
                break;
         etc.
         }
    }
    You shouldn't need to return an explicit string. An implicit conversion will be made for you. I tested it both ways under the MSVC++ 6.0 environment and they both work.
    "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

  14. #14
    uninteresting
    Join Date
    Jun 2002
    Posts
    66
    Quote Originally Posted by hk_mp5kpdw
    You shouldn't need to return an explicit string. An implicit conversion will be made for you. I tested it both ways under the MSVC++ 6.0 environment and they both work.
    Oh okay, thanks for the clarification.

    I realise how it's "adjusting" to my post, but it's really ugly and would be nicer if it just stayed the same
    *** TITANIC has quit (Excess Flood)

  15. #15
    Unregistered User Overload's Avatar
    Join Date
    Jul 2004
    Posts
    12
    Quote Originally Posted by hk_mp5kpdw
    You shouldn't need to return an explicit string. An implicit conversion will be made for you. I tested it both ways under the MSVC++ 6.0 environment and they both work.
    yea, cuz my reasoning behind was because I dont write:

    return int (0);



    P.S. FYI cuz i'm just a smart ass like that, Tman you quoted the wrong person.
    ._«:¤©•®™ª°*¯*°ª™®•©¤:»_. C++ ._«:¤©•®™ª°*¯*°ª™®•©¤:»_.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Does returning newly-declared strings cause leaks?
    By Anvilsmith in forum C++ Programming
    Replies: 8
    Last Post: 07-29-2008, 11:01 AM
  2. Returning strings?
    By DBProgrammer in forum C++ Programming
    Replies: 4
    Last Post: 09-26-2007, 07:15 PM
  3. correct way of returning strings
    By marsface in forum C++ Programming
    Replies: 5
    Last Post: 06-11-2003, 10:33 AM
  4. Passing & Returning Strings from Functions
    By nisaacs in forum C Programming
    Replies: 1
    Last Post: 01-30-2002, 05:34 AM
  5. returning strings
    By Unregistered in forum Windows Programming
    Replies: 1
    Last Post: 09-06-2001, 10:38 PM