Start New Discussion within our Software Development Community

I have a program evaluating a straight hand in poker: Here is my code:

int numInRank[14];

cin.get(rankCh);
    switch (toupper(rankCh))
     {
      case '-1':            exit(0);
      case '?':           rank = 0; break;
      case 'A':          rank = 1; break;
      case '2':           rank = 2; break;
      case '3':           rank = 3; break;
      case '4':           rank = 4; break;
      case '5':           rank = 5; break;
      case '6':           rank = 6; break;
      case '7':           rank = 7; break;
      case '8':           rank = 8; break;
      case '9':           rank = 9; break;
      case 'T':           rank = 10; break;
      case 'J':           rank = 11; break;
      case 'Q':          rank = 12; break;
      case 'K':          rank = 13; break;

bool isStraight(int numInRank[]){
    int count = 0;
    for(int i = 1; i  < 14; i++){
        if(numInRank[i] != 0){
            count++;
            if(i == 13 && numInRank[1] != 0) count++;
        }else{
            count = 0;
        }
        if(count == 5) return true;
    }
    return false;
}

I would like to modify the above code to handle A high and A low straights- I sized my array to 14 instead of 13 to hold both...
Any suggestions...

#include <iostream>
#include <cctype>
#include <cstring>
#include <algorithm>
using namespace std ;
 
const char* const ranks = "A23456789TJQK ATJQK" ; 
struct on_rank 
{ 
 bool operator() ( char a, char b ) const
 { return strchr(ranks,a) < strchr(ranks,b) ; }
};
 
int main()
{
 enum { NCARDS = 5 };
 char cards[NCARDS+1] = {0} ;
 for( int i=0 ; i<NCARDS ; ++i ) 
  { cin >> cards[i] ; cards[i] = toupper(cards[i]) ; }
 sort( cards, cards+NCARDS, on_rank() ) ;
 cout << "straight? " << boolalpha 
        << ( strstr( ranks, cards ) != 0 ) << '\n' ;
}
This article has been dead for over six months. Start a new discussion instead.