A development process
Then break your 200+ line main() into more manageable blocks of code.
Second, if you're going to have massive functions, then indentation is absolutely vital.
Code:
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main(void)
{
char inputtedhand[64];
int NumC = 0, NumD = 0, NumS = 0, NumH = 0;
int handcount = 0;
int tempvar;
char cardvalue[10];
int i = 0, n = 0, m = 0, z = 0;
int index = 0, counter = 0;
char handscore[5];
while (counter++ < 2) {
scanf("%15c", inputtedhand);
memset(cardvalue, '\0', sizeof(cardvalue));
NumS = 0, NumD = 0, NumC = 0, NumH = 0, index = 0;
if (counter == 1) {
printf("The hand for Player 1 is:\n");
} else if (counter == 2) {
printf("\nThe hand for player 2 is:\n");
}
for (i = 0; i < 15; i += 3) {
m = (i + 1); //SUIT
n = (m + 1); //SPACE
inputtedhand[i] = toupper(inputtedhand[i]);
inputtedhand[m] = toupper(inputtedhand[m]);
if (inputtedhand[i] == 50) {
printf("The two of ");
tempvar = 2;
/* The the first value if a 2, "The two of" will be dsplayed at the output, and the number 2 will be stored as a temporary variable for later */
} else if (inputtedhand[i] == 51) {
printf("The three of ");
tempvar = 3;
} else if (inputtedhand[i] == 52) {
printf("The four of ");
tempvar = 4;
} else if (inputtedhand[i] == 53) {
printf("The five of ");
tempvar = 5;
} else if (inputtedhand[i] == 54) {
printf("The six of ");
tempvar = 6;
} else if (inputtedhand[i] == 55) {
printf("The seven of ");
tempvar = 7;
} else if (inputtedhand[i] == 56) {
printf("The eight of ");
tempvar = 8;
} else if (inputtedhand[i] == 57) {
printf("The nine of ");
tempvar = 9;
} else if (inputtedhand[i] == 48) {
printf("The ten of ");
tempvar = 10;
} else if (inputtedhand[i] == 74) {
printf("The Jack of ");
tempvar = 11;
} else if (inputtedhand[i] == 81) {
printf("The Queen of ");
tempvar = 12;
} else if (inputtedhand[i] == 75) {
printf("The King of ");
tempvar = 13;
} else if (inputtedhand[i] == 65) {
printf("The Ace of ");
tempvar = 14;
} else {
printf("%c", inputtedhand[i]);
printf("is not recognised ");
}
if (inputtedhand[m] == 67) {
printf("Clubs\n");
NumC++;
/* This will complete the name for the first card, eg. "The two of Clubs" and counts the number of clubs there has been, this will be useful then looking for flushes */
} else if (inputtedhand[m] == 68) {
printf("Diamonds\n");
NumD++;
} else if (inputtedhand[m] == 72) {
printf("Hearts\n");
NumH++;
} else if (inputtedhand[m] == 83) {
printf("Spades\n");
NumS++;
} else {
printf(" SUIT ");
printf("%c", inputtedhand[m]);
printf(" not recognised\n");
}
cardvalue[index] = tempvar;
index++;
} //End of FOR loop
int flush = 0;
if ((NumH == 5) || (NumC == 5) || (NumD == 5) || (NumS == 5)) {
flush = 1;
} else {
flush = 0;
}
int outside, inside, first, second;
{
for (outside = 4; outside > 0; outside--) {
for (inside = 0; inside < outside; inside++) {
first = cardvalue[inside];
second = cardvalue[inside + 1];
if (first > second) {
cardvalue[inside] = second;
cardvalue[inside + 1] = first;
}
}
}
}
printf("%d", cardvalue[0]);
printf(" (0)\n");
printf("%d", cardvalue[1]);
printf(" (1)\n");
printf("%d", cardvalue[2]);
printf(" (2)\n");
printf("%d", cardvalue[3]);
printf(" (3)\n");
printf("%d", cardvalue[4]);
printf(" (4)\n");
int pair = 0, pair1 = 0, pair2 = 0, pair3 = 0, pair4 = 0;
if (cardvalue[0] == cardvalue[1]) {
pair1++;
pair++;
}
if (cardvalue[1] == cardvalue[2]) {
pair2++;
pair++;
}
if (cardvalue[2] == cardvalue[3]) {
pair3++;
pair++;
}
if (cardvalue[3] == cardvalue[4]) {
pair4++;
pair++;
}
int straight = 0, straightflush = 0, royalflush = 0, fourkind = 0, fullhouse = 0, threekind =
0, twopair = 0, highcard = 1;
if ((cardvalue[0] + 1 == cardvalue[1]) && (cardvalue[1] + 1 == cardvalue[2])
&& (cardvalue[2] + 1 == cardvalue[3]) && (cardvalue[3] + 1 == cardvalue[4])) {
if (flush == 1) {
if (cardvalue[0] == 10) {
royalflush++;
// printf("getting here royalflush");
} else {
straightflush++;
//printf("getting here straightfflush");
}
} else {
straight++;
//printf("getting here straight");
}
} else {
if ((pair1 == 1 && pair2 == 1 && pair3 == 1) || (pair4 == 1 && pair3 == 1 && pair2 == 1)) {
fourkind++;
} else if ((pair1 == 1 && pair2 == 1 && pair4 == 1)
|| (pair1 == 1 && pair3 == 1 && pair4 == 1)) {
fullhouse++;
} else if ((pair1 == 1 && pair2 == 1) || (pair2 == 1 && pair3 == 1)
|| (pair3 == 1 && pair4 == 1)) {
threekind++;
} else if ((pair1 == 1 && pair3 == 1) || (pair1 == 1 && pair4 == 1)
|| (pair2 == 1 && pair4 == 1)) {
twopair++;
} else if (flush == 1) {
flush++;
} else {
highcard++;
}
}
while (1) {
if ((royalflush) >= 1) {
printf("The hand is a Royal Flush\n");
handscore[counter] = (180 + cardvalue[4]);
break;
} else if (straightflush == 1) {
printf("The hand is the Straight Flush\n");
handscore[counter] = 160 + cardvalue[3];
break;
} else if (fourkind >= 1) {
printf("The hand is a Four of a Kind\n");
handscore[counter] = 140 + cardvalue[4];
break;
} else if (fullhouse >= 1) {
printf("The hand is a Full House\n");
handscore[counter] = 120 + cardvalue[4];
break;
} else if (flush >= 1) {
printf("The hand is a Flush\n");
handscore[counter] = 100 + cardvalue[4];
break;
} else if (straight >= 1) {
printf("The hand is a Straight\n");
handscore[counter] = 80 + cardvalue[3];
break;
} else if (threekind >= 1) {
printf("The hand is a Three of a Kind\n");
handscore[counter] = 60 + cardvalue[4];
break;
} else if (twopair >= 1) {
printf("The hand is a Two Pair\n");
handscore[counter] = 40 + cardvalue[4];
break;
} else if (pair >= 1) {
printf("The hand is a Pair\n");
handscore[counter] = 20 + cardvalue[4];
break;
} else if (highcard >= 1) {
printf("\n The hand is a High Card\n");
handscore[counter] = cardvalue[4];
break;
} else {
printf("\n The hand scores 0 (this should not happen)");
/* This is to ensure the above has been executed as planned, if this comes uop then there is an error */
}
}
} //End of WHILE loop
if (handscore[0] > handscore[1]) {
printf("\nPlayer 1 wins");
} else if (handscore[0] == handscore[1]) {
printf("\ndraw");
} else if (handscore[1] > handscore[1]) {
printf("\nPlayer 2 wins");
} else {
printf("This shouldn't happen");
}
printf("\n%d\n", handscore[1]);
printf("%d", handscore[0]);
} //End of program
Three, beware of uninitialised variables.
Code:
if (counter == 1) {
printf("The hand for Player 1 is:\n");
} else if (counter == 2) {
printf("\nThe hand for player 2 is:\n");
}
Here, you seem to know that counter is either 1 or 2.
But here...
Code:
if (handscore[0] > handscore[1]) {
printf("\nPlayer 1 wins");
} else if (handscore[0] == handscore[1]) {
printf("\ndraw");
} else if (handscore[1] > handscore[1]) {
printf("\nPlayer 2 wins");
} else {
printf("This shouldn't happen");
}
You're testing 0 and 1
Now 0 is garbage for sure.
Four, beware of overflow
> handscore[counter] = (180 + cardvalue[4]);
Now for some odd reason, you have an array of chars for this, which might limit you to -128 to 127 (notice this is less than 180).
Consider making it an array of ints.