I'm trying to write program that evaluates a poker hand and returns whether or not it contains a straight, flush, pair, etc. I've got every method except one to work. I cannot figure out how to correctly write the method that determines whether or not there is a straight. Every time I try to complile the code, I get an error saying "DeckOfCards.cpp ISO C++ forbids comparison between pointer and integer."

DeckOfCards::DeckOfCards()
{
   char *suitValue[ 4 ] = { "Hearts", "Diamonds", "Clubs", "Spades" };
   char *faceValue[ 13 ] = { "Ace", "Deuce", "Three", "Four", "Five", 
      "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King" }; 

   // initialize suit
   for ( int i = 0; i < 4; i++ )
      suit[ i ] = suitValue[ i ];

   // initialize face
   for ( int i = 0; i < 13; i++ )
      face[ i ] = faceValue[ i ];

   // loop through rows of deck
   for ( int row = 0; row <= 3; row++ )
   {
      // loop through columns of deck for current row
      for ( int column = 0; column <= 12; column++ )
      {
         deck[ row ][ column ] = 0; // initialize slot of deck to 0
      } // end inner for
   } // end outer for
   
   srand( time( 0 ) ); // seed random number generator
} // end DeckOfCards default constructor

void DeckOfCards::shuffle()
{
   int row; // represents suit value of card
   int column; // represents face value of card

   // for each of the 52 cards, choose a slot of the deck randomly
   for ( int card = 1; card <= 52; card++ ) 
   {
      do // choose a new random location until unoccupied slot is found
      {
         row = rand() % 4; // randomly select the row
         column = rand() % 13; // randomly select the column
      } while( deck[ row ][ column ] != 0 ); // end do...while

      // place card number in chosen slot of deck
      deck[ row ][ column ] = card;
   } // end for
} // end function shuffle

// deal a five card poker hand
void DeckOfCards::deal()
{
   // loop to distrubute the cards
   for ( int card = 1; card < 6; card++ )

      for ( int row = 0; row <= 3; row++ )

         for ( int column = 0; column <= 12; column++ )

            if ( deck[ row ][ column ] == card ) 
            {
               hand[ handPos ][ 0 ] = row;
               hand[ handPos ][ 1 ] = column;
               
	       cout << setw( 5 ) << face[ column ] << (" of ")  
                  	<< setw( 8 ) << left << suit[ row ] << endl; 
               handPos++;
            } // end if
} // end deal

void DeckOfCards::straight()
{
   for ( int i = 0; i < 4; i++ )
   {  
      if ( hand [ i ] == 1 && hand [ i ] == 2 && hand [i] == 3 && 
           hand [ i ] == 4 && hand [ i ] == 5 )
      {
         cout <<("\n You have a straight");
      }
      else if ( hand [ i ] == 2 && hand [ i ] == 3 && hand [i] == 4 &&
                hand [ i ] == 5 && hand [ i ] == 6 )
      {
         cout <<("\nYou have a straight");
      }
      else if ( hand [ i ] == 3 && hand [ i ] == 4 && hand [i] == 5 && 
                hand [ i ] == 6 && hand [ i ] == 7 )
      {
         cout <<("\nYou have a straight");
      }
      else if ( hand [ i ] == 4 && hand [ i ] == 5 && hand [i] == 6 && 
                hand [ i ] == 7 && hand [ i ] == 8)
      {
         cout <<("\nYou have a straight");
      }
      else if ( hand [ i ] == 5 && hand [ i ] == 6 && hand [i] == 7 && 
                hand [ i ] == 8 && hand [ i ] == 9 )
      {
         cout <<("\nYou have a straight");
      }
      else if ( hand [ i ] == 6 && hand [ i ] == 7 && hand [i] == 8 && 
                hand [ i ] == 9 && hand [ i ] == 10 )
      {
         cout <<("\nYou have a straight");
      }
      else if ( hand [ i ] == 7 && hand [ i ] == 8 && hand [i] == 9 &&
                hand [ i ] == 10 && hand [ i ] == 11 )
      {
         cout <<("\nYou have a straight");
      }
      else if ( hand [ i ] == 8 && hand [ i ] == 9 && hand [i] == 10 &&
                hand [ i ] == 11 && hand [ i ] == 12 )
      {
         cout <<("\nYou have a straight");
      }
      else if ( hand [ i ] == 9 && hand [ i ] == 10 && hand [i] == 11 &&
                hand [ i ] == 12 && hand [ i ] == 13 )
      {
         cout <<("\nYou have a straight");
      }
   }
}

Looks like whatever is item in hand is not an integer. I also fail to see how hand can be 1, 2, 3 , 4, ... all at the same time?

Looks like whatever is item in hand is not an integer. I also fail to see how hand can be 1, 2, 3 , 4, ... all at the same time?

Well, I thougt it was an integer. The char array assigns values to each card. I think part of my problem is hand. I'm trying to see if the hand contains all of the values, but the values don't have to be in any order. To do that, I need to advance 'i' to check each hand. I'm just not sure how to do that.

hand appears to be a 2-dimensional array, in which case you need to compare each element (if you use only one subscript, you're comparing a pointer to an integer)

if (hand[i][0]==1 && hand[i][1]==2 && /*...*/)

Of course, the problem here is that the cards may not be in order, so my code won't work correctly unless the array is sorted.

Thank you so much for helping me!!!! I added a sort method (a simple selection sort) to my code and fixed the straight method, but something still is not working. I think something is wrong with my sort method. Any ideas as to what I am doing wrong? Thanks in advance!

void sortHand( char * const);
void swap (int * const, int * const);
 
// DeckOfCards default constructor initializes deck
DeckOfCards::DeckOfCards()
{
   // initialize suit and face arrays 
   char *suitValue[ 4 ] = { "Hearts", "Diamonds", "Clubs", "Spades" };
   char *faceValue[ 13 ] = { "Ace", "Deuce", "Three", "Four", "Five", 
      "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King" }; 
   for ( int i = 0; i < 4; i++ )
      suit[ i ] = suitValue[ i ];
   for ( int i = 0; i < 13; i++ )
      face[ i ] = faceValue[ i ];
   // loop through rows of deck
   for ( int row = 0; row <= 3; row++ )
   {
      // loop through columns of deck for current row
      for ( int column = 0; column <= 12; column++ )
      {
         deck[ row ][ column ] = 0; // initialize slot of deck to 0
      } // end inner for
   } // end outer for
 
   srand( time( 0 ) ); // seed random number generator
} // end DeckOfCards default constructor
 
// shuffle cards in deck
void DeckOfCards::shuffle()
{
   int row; // represents suit value of card
   int column; // represents face value of card
   // for each of the 52 cards, choose a slot of the deck randomly
   for ( int card = 1; card <= 52; card++ ) 
   {
      do // choose a new random location until unoccupied slot is found
      {
         row = rand() % 4; // randomly select the row
         column = rand() % 13; // randomly select the column
      } 
      while( deck[ row ][ column ] != 0 ); // end do...while
      // place card number in chosen slot of deck
      deck[ row ][ column ] = card;
   } // end for
} // end function shuffle
 
// Deal a five card poker hand.
void DeckOfCards::deal()
{
   int handPos = 0; // Hand Position 
   cout << "\nTHE PLAYER'S HAND:\n\n";
 
   sortHand( faceValue ); // Sort the array
 
   for ( int card = 1; card < 6; card++ )
   {
      for ( int row = 0; row <= 3; row++ )
      {
         for ( int column = 0; column <= 12; column++ )
         {
            if ( deck[ row ][ column ] == card ) 
            {
               hand[ handPos ][ 0 ] = row;
               hand[ handPos ][ 1 ] = column;
 
            cout << setw( 5 ) << face[ column ] << (" of ")  
             << setw( 8 ) << left << suit[ row ] << endl; 
               handPos++;
            } // end if statement
         } // end for loop
      } // end for loop
   } // end for loop
} // end deal
 
// See if the deck of cards contains a sraight.
void DeckOfCards::straight()
{
   for ( int i = 0; i < 4; i++ )
   {
      if ( hand[i][0] == 1 && hand[i][1] == 2 && hand[i][2] == 3 && hand[i][3] == 4 && hand[i][4] == 5 )
      {
         cout << ("\nThe hand contains a straight.\n");
      }
      else if ( hand[i][0] == 2 && hand[i][1] == 3 && hand[i][2] == 4 && hand[i][3] == 5 && hand[i][4] == 6 )
      {
         cout << ("\nThe hand contains a straight.\n");
      }
      else if ( hand[i][0] == 3 && hand[i][1] == 4 && hand[i][2] == 5 && hand[i][3] == 6 && hand[i][4] == 7 )
      {
         cout << ("\nThe hand contains a straight.\n");
      }
      else if ( hand[i][0] == 4 && hand[i][1] == 5 && hand[i][2] == 6 && hand[i][3] == 7 && hand[i][4] == 8 )
      {
         cout << ("\nThe hand contains a straight.\n");
      }
      else if ( hand[i][0] == 5 && hand[i][1] == 6 && hand[i][2] == 7 && hand[i][3] == 8 && hand[i][4] == 9 )
      {
         cout << ("\nThe hand contains a straight.\n");
      }
      else if ( hand[i][0] == 6 && hand[i][1] == 7 && hand[i][2] == 8 && hand[i][3] == 9 && hand[i][4] == 10 )
      {
         cout << ("\nThe hand contains a straight.\n");
      }
      else if ( hand[i][0] == 7 && hand[i][1] == 8 && hand[i][2] == 9 && hand[i][3] == 10 && hand[i][4] == 11 )
      {
         cout << ("\nThe hand contains a straight.\n");
      }
      else if ( hand[i][0] == 8 && hand[i][1] == 9 && hand[i][2] == 10 && hand[i][3] == 11 && hand[i][4] == 12 )
      {
         cout << ("\nThe hand contains a straight.\n");
      }
      else if ( hand[i][0] == 9 && hand[i][1] == 10 && hand[i][2] == 11 && hand[i][3] == 12 && hand[i][4] == 13 )
      {
         cout << ("\nThe hand contains a straight.\n");
      }
   }
} 
 
void sortHand( char * const faceValue)
{
   int smallest;
 
   // loop over size - 1 elements
   for ( int i = 0; i < 13; i++ );
   {
       smallest = i; // first index of remaining array
 
       // loop to find index of smallest element  
       for ( int index = i + 1; index < 13; index++ )
 
           if ( faceValue [index] < faceValue [smallest] )
                smallest = index;
       swap( &faceValue [ i ], &faceValue[ smallest ] );
   }
}
 
void swap ( char * const element1Ptr, char * const element2Ptr )
{
     int hold = *element1Ptr;
     *element1Ptr = *element2Ptr;
     *element2Ptr = hold;
}

One of those little errors, it appears:

for ( int i = 0; i < 13; i++ ); //<--semicolon

See if that works when you remove the trailing semicolon.

As a side note, you should probably prefer passing by reference to functions like your swap function, that is:

void swap ( char& element1Ptr, char& element2Ptr )
{
     char hold = element1Ptr;
     element1Ptr = element2Ptr;
     element2Ptr = hold;
}
//...call it like this:
swap( faceValue[i], faceValue[smallest]);

straight() is a terrible function. What about something like

isStraight=true;
for (i=0; i<8; i++)
{
    for (j=0; j<5; j++)
    {
        if (hand[j+1] != hand[j])
        {
            isStraight = false;
            break;
        }
        if (isStraight == false)
        {
            break;
        }
     }
}

Of course modified for your design...

I removed the semicolon, but the sort still will not work. The compiler error is

DeckOfCards.cpp `faceValue' undeclared (first use this function)

and it is saying that the error is in this line

sortHand( faceValue ); // Sort the array

.

I tried to fix the problem by changing the arrays to

static const char *suitValue[ 4 ] = { "Hearts", "Diamonds", "Clubs", "Spades" };
    static const char *faceValue[ 13 ] = { "Ace", "Deuce", "Three", "Four", "Five", "Six", "Seven",
                                          "Eight", "Nine", "Ten", "Jack", "Queen", "King" };

I tried to call the sort method to sort the array by doing this,

sortHand( char const *faceValue ); // Sort the array

but there is still a problem.

I tried to fix the problem by changing the arrays to
I tried to call the sort method to sort the array by doing this,

sortHand( char const *faceValue ); // Sort the array

but there is still a problem.

I dont if if its just me but how can you expect a procedure to sort a data item for you which involves manipulating teh data by passing the argument as constant ??? In you function prototype the const qualifier applies to the char array data and not the pointer if thats what you wnated to do ?

Sorting your facevalues won't work, you are sorting strings that when sorted will in no way represent the proper sequence for a straight!

You can give them integer values from 2 to 13, and notice that the ace will be a 1 or a 13 in a straight.

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.