Thursday, July 30, 2009

How do I get a function to return a character in c++?

I am creating a black jack game and when it randomly chooses a number from 1-13 (for the cards) I want it to return a character if it picks 13.





K for 13


Q for 12


J for 11


A for 1





but my skills are limited and I have no idea how to do this, because if I try


return ("Q")





it says


"cannot convert from 'const char [2]' to 'int'"





here is the function





int randmax ( int max )


{


if ( max=cards )


{


static int seed = 2; /* determines sequence of random numbers */


/* initialized to 2 here to meet project */


/* requirements */


seed = (seed * 13077 + 6925) % 32768;


card = static_cast%26lt;int%26gt; (max * seed / 32768.0 + 1.0);


if ( card %26gt;= 2 %26amp;%26amp; card %26lt;= 10)


return(card);


if ( card == 13)


return("K");


if ( card == 12)


return("Q");


if ( card == 11)


return("J");


if ( card == 1)


return("A");


}








}

How do I get a function to return a character in c++?
The best way to do this is to make an array of strings like...


string cards[13];





and set them to = "A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"





Then when you pick a number x from 0 to 12 just return the array value at that index





return cards[x];





Anything else is just way over-complicating this issue. The whole function could be 4 lines including the function declaration and closing brace.





public string drawCard() {


string cards[13] = {"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"};


return cards[(rand()%13)];


}





The only difference here is that you'll be getting a string value instead of an int. Also, a more realistic method to this would be to create a Deck class which has an array of Card objects and actually create a random order in the array so that you could 'draw' cards from the Deck and not pick a random value each time. Theoretically, with the random method, in a game you could draw more than 4 Aces etc.
Reply:You need to convert the numbers (1-10) into their string equivalent ("1", "2", ... "10") and return a string.





Otherwise, return the numerical value (1-13) and convert it to a string value of ("J" ... "K") when you need to display it (ie just keep in mind that numbers above 10 will be represented by a letter when it's time to display things.)
Reply:Instead of using the return function use this instead:





#include %26lt;iostream%26gt;


int randmax ( int max )


{


if ( max=cards )


{


static int seed = 2;/* determines sequence of random numbers */


/* initialized to 2 here to meet project */


/* requirements */


seed = (seed * 13077 + 6925) % 32768;


card = static_cast%26lt;int%26gt; (max * seed / 32768.0 + 1.0);


if ( card %26gt;= 2 %26amp;%26amp; card %26lt;= 10)


return(card);


if ( card == 13)


cout %26lt;%26lt; "K";


if ( card == 12)


cout %26lt;%26lt; "Q";


if ( card == 11)


cout %26lt;%26lt; "J";


if ( card == 1)


cout %26lt;%26lt; "A";


}








}

flower pictures

No comments:

Post a Comment