Hey there!

Summarizing the problem within the title is too hard but I tried.

Let's just jump right in!

I am pretty new (enough to only program in the console) to C++. Right now I am trying to code a hi-lo game. Where you guess if the next card is higher or lower than the current.

But my problem is that I want to use a function for simple access to the randomize function and I have 1 set up for the house and 1 for the card.

What the card one does is randomizing a value between 1 and 13. But what I want to do is that I want to return J instead of 11, Q instead of 12 etc.

My first idea was to just use a char as the card, as then it wouldn't matter if it's a 1, 5, J or A. But then if the random function gives me a 10 it will crash.

I don't know how I should do to make it as simple as possible. I was thinking about using a vector as I can just resize it if necessary. But maybe there's a different way?

Here's the code if you feel like you need to look at it :

``````int iRandomCard() {
int randomCard;

time_t t;
time(&t);
srand(t);

randomCard = rand()%13+1;

return randomCard;
}

void vStartGame() {
string house;
int card;

house = sRandomHouse();
card = iRandomCard();
}``````

(obviously using int won't work but that's just the code right now)

I tend to make posts long...

Help is appreciated!

You could do it with an enumeration type but the easiest way to go about it would be to store it as an integer (as you have) and only convert it to jack, queen, king, ace when it's displayed.

``````if(card == 11)
std::cout<<"Jack of "<<house<<std::endl;
else if (card == 12)
//queen of
else if (card == 13)
else if (card == 1)
else
std::cout<<card<<" of "<<house<<std::endl;``````

You could do it with an enumeration type but the easiest way to go about it would be to store it as an integer (as you have) and only convert it to jack, queen, king, ace when it's displayed.

``````if(card == 11)
std::cout<<"Jack of "<<house<<std::endl;
else if (card == 12)
//queen of
else if (card == 13)
else if (card == 1)
else
std::cout<<card<<" of "<<house<<std::endl;``````

Yes, I could do that. But it would be a lot easier only having 1 type to keep track of. Else I would have to check if it's 11 and create a new char/string and print that instead.

Use an enum. Define all the 13 cards in the enum

``````enum card
{
one=1,
two=2,

......
jack=11,
queen=12
};``````

For the purposes of your program it would only be one type. The user would never see the 1 thru 13 holding the value. Output can be placed in a "helper" function where you pass in card and house and you get a string back as above.
Otherwise check out something like this.

I'm an array kind of guy (enums will work just fine too). This code snippet might give you some ideas. Look at lines 42 - 44 and lines 175 - 182. Random indexes are generated, then get your card by looking up the array value for that array index.

Yes, I could do that. But it would be a lot easier only having 1 type to keep track of. Else I would have to check if it's 11 and create a new char/string and print that instead.

Not true. Just create a name string array available for all displays: `string cardDisplay[] = {"Ace", "2", "3",...,"10","Jack"...};` then each and every time you display the card, use `cardDisplay[card]`

commented: Works too! +2

Not true. Just create a name string array available for all displays: `string cardDisplay[] = {"Ace", "2", "3",...,"10","Jack"...};` then each and every time you display the card, use `cardDisplay[card]`

Oh? I thought the compiler would whine if you assigned a number to a string. Guess I should have tried that before.

Anyways I haven't learned enums yet but I guess I'll have to learn those sooner or later so might as well start now

Thanks for the help!

Oh? I thought the compiler would whine if you assigned a number to a string

It WILL whine if you assign a number to a string. I am suggesting that you use a string array. The number is the array INDEX. I think WaltP is suggesting the same thing:

``````string cards[] = {"Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10",
"Jack", "Queen", "King"};
int cardIndex = rand () % 13;
cout << cards[cardIndex]; // if cardIndex is 11, "Queen" is displayed``````

You think? "2" is not a number, it is a string that has a number in it. Double quotes define a string. Any character is allowed, including the characters '0' - '9' -- they are characters, not numbers. 0-9 are numbers. (note the quotes, that's makes all the difference.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.