# Thread: Checking for a straight (poker) in C++?

1. ## Checking for a straight (poker) in C++?

Hi, I'm making a poker game where the user is provided with 5 cards and these cards are given a value. Unfortunately I've been having errors when the program checks for a straight. To check for a straight the program orders the card numbers in ascending order, checks if 5 of them are in chronological order and then gives them a value of 54 if they are.
For some reason cards which are not in chronological order return the value of 54.

Each card is valued from 2 - 14 (2 being 2, and 14 being an ace, that is why had to add
Code:
`maxnum == 14 && minnum == 2 == (mminnum - 1) == (midnum - 2) == (mmaxnum - 3`
)
Here is the full check:
Code:
```    //CHECK FOR STRAIGHT
int minnum;
int mminnum;
int midnum;
int mmaxnum;
int maxnum;
int noStraight = 0;
for (int i = 0; i < 15; i++)
{
if (i == card1num || i == card2num || i == card3num || i == card4num || i == card5num)
{
minnum = i;
break;
}
}
for (int i = 0; i < 16; i++)
{
if (i == card1num || i == card2num || i == card3num || i == card4num || i == card5num && i != minnum)
{
mminnum = i;
break;
}
if (i == 15)
{
noStraight = 1;
}
}
for (int i = 0; i < 16; i++)
{
if (i == card1num || i == card2num || i == card3num || i == card4num || i == card5num && i != mminnum && noStraight == 0)
{
midnum = i;
break;
}
if (i == 15)
{
noStraight = 1;
}
}
for (int i = 0; i < 16; i++)
{
if (i == card1num || i == card2num || i == card3num || i == card4num || i == card5num && i != midnum && noStraight == 0)
{
mmaxnum = i;
break;
}
if (i == 15)
{
noStraight = 1;
}
}
for (int i = 0; i < 16; i++)
{
if (i == card1num || i == card2num || i == card3num || i == card4num || i == card5num && i != mmaxnum && noStraight == 0)
{
maxnum = i;
break;
}
if (i == 15)
{
noStraight = 1;
}
}

if (minnum == (mminnum - 1) == (midnum - 2) == (mmaxnum - 3) == (maxnum - 4) || maxnum == 14 && minnum == 2 == (mminnum - 1) == (midnum - 2) == (mmaxnum - 3))
{
cardsval = 54;
}```
Could someone please tell me what I have done wrong here (why some card numbers not in chronological order are being counted as a straight?

If possible please don't tell me to use any tools which I have not already used in this program (arrays, pointers etc) as I am getting through a book on C++ and this is one of the challenges at the end of a chapter (you are expected to be able to write the program using only the knowledge of C++ which I already have).

2. > if (i == card1num || i == card2num || i == card3num || i == card4num || i == card5num && i != minnum)
The logic on this reads as
if (i == card1num || i == card2num || i == card3num || i == card4num || ( i == card5num && i != minnum ) )

This might not be what you want.

3. thanks for the reply but no that does not fix the problem

4. Since I don't have time to guess which combination of cards which are NOT a straight, but manage to get through your code as being a straight, perhaps you could post that information.

Popular pages Recent additions