Return a String

This is a discussion on Return a String within the C++ Programming forums, part of the General Programming Boards category; how do I create a function that returns a string? I'm not sure how to declare it in the file ...

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    5

    Return a String

    how do I create a function that returns a string? I'm not sure how to declare it in the file header, as coding "string function(int x)" does not work.

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,169
    Code:
    std::string my_function(int x);
    Make sure you include the proper header (<string>) before the declaration!
    And do not use using namespace std; in the header (you may use it in your .cpp file).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  3. #3
    Registered User
    Join Date
    Jul 2010
    Posts
    5
    Yeah, tried that too, but I get an error when compliling.

    " 'std' : is not a class or namespace name"

    Do I need to add an include statement?

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,169
    Yes, <string>.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  5. #5
    Registered User
    Join Date
    Jul 2010
    Posts
    5
    what is the proper syntax in calling this function and returning a string to the main program? do i need to send the string into the function as a variable?
    Code:
    string=function(x);
    
    function(x){
    
      return(?);
    }

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,169
    Why would you need to pass it in? You just need to return something that is of proper type.
    Example:
    Code:
    #include <string>
    #include <iostream>
    
    std::string foo(int x)
    {
        switch (x)
        {
            case 0: return "Hello";
            case 1: return "World!";
            default: return "Bad input";
        }
    }
    
    int main()
    {
        std::cout << foo(0) << " " << foo(1) << std::endl;
    }
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  7. #7
    Registered User
    Join Date
    Jul 2010
    Posts
    5
    i'm getting an error:

    "error LNK2001: unresolved external symbol "public: class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __thiscall COddsDlg::GetOdds(double)" (?GetOdds@COddsDlg@@QAE?AV?$basic_string@DU?$char_ traits@D....1 unresolved externals"

    this is what i have:


    in header:
    Code:
    std::string odds;
    std::string GetOdds(double percentage);
    in Main:
    Code:
    odds=GetOdds(percentage);
    ...
    
    std::string GetOdds(double percentage){
    
    	std::string converted_odds;
    	std::stringstream ss;
    	double temp_Odds;
    	temp_Odds=(1.0/percentage)-1.0;
    
    	ss << std::fixed << std::setprecision(1) << percentage*100.0;
    	converted_odds=ss.str();
    
    	return converted_odds;
    }
    any idea why?

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,169
    "error LNK2001: unresolved external symbol "public: class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __thiscall COddsDlg::GetOdds(double)" (?GetOdds@COddsDlg@@QAE?AV?$basic_string@DU?$char_ traits@D....1 unresolved externals"
    Let me guess. You have something like:
    Code:
    class COddsDlg
    {
        //...
        std::string GetOdds(double percentage);
        // ...
    }
    
    std::string GetOdds(double percentage){
        // ...
    }
    Right?
    See the declaration is inside the class? The linker is trying to find it, but it doesn't exist, because the function you defined is global.
    You have to tell the compiler that it's actually part of the class:
    Code:
    std::string COddsDlg::GetOdds(double percentage){
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  9. #9
    Registered User
    Join Date
    Jul 2010
    Posts
    5
    how can i clear odds each time? i tried the following and neither worked:

    odds = NULL;

    odds.clear();

  10. #10
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,169
    NULL (or rather nullptr) is only for pointers, not strings.
    For strings, you can simply assign "" (str = "";).
    clear should work too. I am guessing the error is elsewhere.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  11. #11
    Registered User
    Join Date
    Apr 2006
    Posts
    2,011
    Likely you're trying to clear a local copy of odds, instead of the version that's in side the class.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  12. #12
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,584
    If I'm going to return a string from a class method I normally ensure that string is a private or protected member of the class. Then I can return a const reference to the string or I can return the const char * equivalent of the string (string::c_str()) since the string will not go out of scope in the function call. If not then you can return std::string and short of being in a time critical render loop you probably won't have any issues with it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can you help me about tolower() in file
    By nctar in forum C Programming
    Replies: 7
    Last Post: 05-12-2010, 10:04 AM
  2. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  3. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  4. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 03:13 PM

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