creating a validation check to ensure the cards entered are correct. It works when the cards are entered correctly the first time. After an invalid entry, the cards store the default value in a switch statement and always cause an error...
Code:
#include <stdio.h>
#include <stdbool.h>
int main()
{
int j, i, k, n = 5, check = 2;
char hands[2][5];
int nhands[2][5];
int first_hand[5];
int second_hand[5];
int count[14];
/* Loop until it is validated */
while(check == 2){
printf("\nPlease enter a hand of 5 card poker for 2 players. Format HHHHH HHHHH\n");
/* Get the cards */
scanf("%c%c%c%c%c %c%c%c%c%c", &hands[0][0], &hands[0][1], &hands[0][2], &hands[0][3], &hands[0][4], &hands[1][0], &hands[1][1], &hands[1][2], &hands[1][3], &hands[1][4]);
/* Change the char values to numerical values for easier testing */
for(j = 0; j<2; j++){
for(i = 0; i<5; i++){
switch(hands[j][i]){
default : nhands[j][i] = 15; break;
case 'A' : nhands[j][i] = 14; break;
case 'K' : nhands[j][i] = 13; break;
case 'Q' : nhands[j][i] = 12; break;
case 'J' : nhands[j][i] = 11; break;
case '0' : nhands[j][i] = 10; break;
case '9' : nhands[j][i] = 9; break;
case '8' : nhands[j][i] = 8; break;
case '7' : nhands[j][i] = 7; break;
case '6' : nhands[j][i] = 6; break;
case '5' : nhands[j][i] = 5; break;
case '4' : nhands[j][i] = 4; break;
case '3' : nhands[j][i] = 3; break;
case '2' : nhands[j][i] = 2; break;
}
}
}
for(k=0; k<14; k++){
count[k] = 0;
check = 1;
}
/* Checks for invalid card types entered */
for(j = 0; j<2; j++){
for(i = 0; i<5; i++){
if(nhands[j][i] == 15){
check = 2;
printf("You entered an invalid card, please retype your hand");
/*debug */
for(j = 0; j<2; j++){
for(i = 0; i<5; i++){
printf("\ncard %d: %d", i+1, nhands[j][i]);
}
}
}
}
}
/* Checks for more than 4 of the same card type */
for(k = 0; k<14; k++){
for(j = 0; j<2; j++){
for(i = 0; i<5; i++){
if(nhands[j][i] == (k+2)){ count[k] = count[k] + 1; }
}
}
if( count[k] > 4) {
check = 2;
printf("You entered too many of the same card, please retype your hand");
}
}
}
getch();
return 0;
}