Thank for most who helped me to find out random fun before..
I am new to programming .. I just writing simple twenty one game..
Now my random out is like that ... k 5 6 q j .. mix char and int together randomly.. it might be suck..! now i want to add evth together to get some value (number) for twenty one game..BUT what i want is to set char with numerial value... like 'k'is 10. for example.. k +5 , the result be 17 etc.. how can set char with numberial value..here is some of my simple code..:lol:

int compcard=3,urcard;//urtotal=0,comptotal=0;
    int num;
    string randomChar = "kqja";
    srand( (unsigned int)time( 0 ) );
    cout<<"u want ?"<<endl;
    cin>>urcard;
    for ( int i = 0; i < urcard; i++ ) {
        if ( rand() < RAND_MAX / 2 )
//in real, ur total .. have to put.. urtotal+=.....
         cout << rand() % 10 + 1 << '\n';
        else
        cout << randomChar[rand() % randomChar.length()]   <<endl;
}
    }

Edited 3 Years Ago by happygeek: fixed formatting

There's a whole load of ways you can do this - depending on the way the rest of your program works, one way you could achieve this would be to set some constants, eg,

const int ace = 1;
const int jack = 10;
const int queen = 10;
const int king = 10;

Another way may be a lookup table where you associate characters with values, and search through that to find the match.

Hi,
Thank for you help.
You meant I have to create linked lists and use searching ..that certain character is already in output.. if true .. set it to concerning value?
Or use vector?
Coz I am not sure how to do it..

Hi,
Thank for you help.
You meant I have to create linked lists and use searching ..that certain character is already in output.. if true .. set it to concerning value?
Or use vector?
Coz I am not sure how to do it..

You could also use the string which is already a part of your program - the position of each character in that string can act as an index reference for a table of card values - eg,

const std::string ranks("ajqk");
    const int values[] = {1,10,10,10};

all you need to do from there is find the position of the letter in the string and use that to find the value of the card.
(You would need to add a check to make sure the letter is actually inside the string first)

yeah i know .. but i dont know how to set value . until now

This article has been dead for over six months. Start a new discussion instead.