returning a string?

This is a discussion on returning a string? within the C++ Programming forums, part of the General Programming Boards category; I've basically been playing with C++ more and more recently and one thing i've came accross is returning a string? ...

  1. #1
    PPN
    PPN is offline
    Registered User
    Join Date
    Oct 2002
    Posts
    3

    returning a string?

    I've basically been playing with C++ more and more recently and one thing i've came accross is returning a string?

    I tried to declare it in my header file while doing the prototypes and the VS .NET doesn't seem to like it, it was fine when i was using char but now i need to return two characters.

    i included the string library so i'm now puzzled.

    Code:
    #include <string>
    using namespace std;
    
    string convert_num(int cnumber) {
    	string temp;
    	cnumber = (cnumber%13);
    	switch (cnumber) {
    		case 0:
    			temp = 'K';
    			break;
    		case 1:
    			temp = 'A';
    			break;
    		case 2:
    			temp = '2';
    			break;
    		case 3:
    			temp = '3';
    			break;
    		case 4:
    			temp = '4';
    			break;
    		case 5:
    			temp = '5';
    			break;
    		case 6:
    			temp = '6';
    			break;
    		case 7:
    			temp = '7';
    			break;
    		case 8:
    			temp = '8';
    			break;
    		case 9:
    			temp = '9';
    			break;
    		case 10:
    			temp = '10';
    			break;
    		case 11:
    			temp = 'J';
    			break;
    		case 12:
    			temp = 'Q';
    			break;
    		default :
    			temp = 'K';
    			break;
    	}
    	return temp;
    }
    and in the head file

    Code:
    string convert_num(int cnumber);
    and yes it is the customary blackjack game that everyone gets asked to do.

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,675
    Your string variable temp is declared locally to the function convert_num. When the function exits, any variable/objects that you declare in such a manner are effectively destroyed and therefore unavailable to any function that calls convert_num and relies upon getting a valid string object returned from it. What you need to do is either pass in an additional string parameter (by pointer/reference) and modify that object in the function, or modify the function to return a pointer to a string object that you dynamically allocate and modify within the convert_num function.
    I used to be an adventurer like you... then I took an arrow to the knee.

  3. #3
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Malmö, Sweden, Sweden
    Posts
    2,070
    Originally posted by hk_mp5kpdw
    Your string variable temp is declared locally to the function convert_num. When the function exits, any variable/objects that you declare in such a manner are effectively destroyed and therefore unavailable to any function that calls convert_num and relies upon getting a valid string object returned from it. What you need to do is either pass in an additional string parameter (by pointer/reference) and modify that object in the function, or modify the function to return a pointer to a string object that you dynamically allocate and modify within the convert_num function.
    What you're saying is very wrong. std::string doesn't work that way at all.

    The only thing he has to do is to replace the singe-quotes (') with double quotes (")
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  4. #4
    PPN
    PPN is offline
    Registered User
    Join Date
    Oct 2002
    Posts
    3
    c:\Documents and Settings\Administrator.SCOTT\My Documents\Visual Studio Projects\blackjack\deck.h(9): error C2146: syntax error : missing ';' before identifier 'convert_num'

    c:\Documents and Settings\Administrator.SCOTT\My Documents\Visual Studio Projects\blackjack\deck.h(9): error C2501: 'string' : missing storage-class or type specifiers
    which is the function prototype, the header file has the following

    Code:
    //
    // Scott MacVicar
    // Black Jack Game
    //
    
    void shuffle ();
    char suit (int cnumber);
    void deal (int array[10], int card);
    string convert_num (int cnumber);

  5. #5
    PPN
    PPN is offline
    Registered User
    Join Date
    Oct 2002
    Posts
    3
    nevermind i feel stupid now

    std::string convert_num (int cnumber);

    works, silly namespaces

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, 01:11 AM
  4. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 09:33 PM

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