i am fixing something and i have that fuction in it

bool DeckOfCards::checkFlush(const Card checkCards)
{
    counter=0;
    cardCheck=" ";
    
    for(int x=1;x<5;x++)
    {
        cardCheck=checkCards.suits[x];
        
        for(int y=0;y<5;y++)
        {
            found=hand[y].find(cardCheck);
                    
            if(found!=string::npos)
            {
                counter++;
            } 
        }
        if(counter==5)
        {
            return true;
        }
        counter=0; 
    }
}

it goes here:

bool result=false;
if((result=checkFlush(checkCards))==true)
    {
        return winner=5;
    }

it returns true all the time even if the counter never gets 5
why???????? :D

Recommended Answers

All 3 Replies

Honestly, I think you're going about it the wrong way. If you adjust your thinking, I think you'll solve your dilemma.

I suggest that you check to see if the counter is greater than 0, but not equal to 5. That way, depending on the value produced, after each iteration of your outer for loop you'll either know that there isn't a flush or that, if there is, it at least isn't in that suit. In other words, look for what is not a flush instead of what is.

If (0<counter && counter<5) , you know you don't have a flush. This logic holds true because you can only have a flush if all 5 cards are the same suit. If even 1 card doesn't match the rest, there is no need to check the rest because you know there isn't one. This allows you to terminate your loop and move on to detecting the next possibility (i.e. straight, 3-of-a-kind, pair, etc.)

Conversely, if (counter==0) all that you know is that there are no cards of that suit in the hand. This information does not preclude the possibility of a flush in a different suit. As a result, you'll have to let the loop iterate again with the next suit.

You don't have any return value for the case where counter does not equal 5, so the caller basically gets whatever junk happens to be in the returned value register - and since any value other than zero (false) evaluates to true, the return value is almost always going to be true.

Actually, I'm surprised that this compiled at all - at the very least it should have given you a warning that not all paths end in a returned value. While C++ is a lot looser than some other languages about this sort of thing - it goes on the assumption that the you know what you're doing - it still should have at least checked to make sure this wasn't an oversight.

Also, in the conditional, the result variable is unnecessary, as is the ' == true ' part, and the winner assignment as well; you could simplify the whole thing as just:

if((checkFlush(checkCards)))
    {
        return 5;
    }

i use netbeans and i suppose thats the reason that dont care about return values.

well the counter i have it as class variable at private,i was clearing counter cause i couldnt understand,what was wrong with it.

thanks both, i study from a book,so its logical to dont have the best ideas for some standard tips.

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.