请求第二次输入后,C中的程序崩溃

请求第二次输入后,C中的程序崩溃,c,C,我是C语言新手,我知道有一些关于程序崩溃的问答,但我认为在这种情况下,我的问答有点具体,我更愿意提出这个问题。我正在尝试一个扑克游戏,在运行程序后,当我输入“Y”或“N”命令时,程序崩溃。如果您能就如何解决这一问题提供任何见解,我将不胜感激。代码如下: #include <stdio.h> #include <time.h> #include <ctype.h> #include <stdlib.h> #define FALSE 0 #defin

我是C语言新手,我知道有一些关于程序崩溃的问答,但我认为在这种情况下,我的问答有点具体,我更愿意提出这个问题。我正在尝试一个扑克游戏,在运行程序后,当我输入“Y”或“N”命令时,程序崩溃。如果您能就如何解决这一问题提供任何见解,我将不胜感激。代码如下:

#include <stdio.h>
#include <time.h>
#include <ctype.h>
#include <stdlib.h>

#define FALSE 0
#define TRUE 1

void printGreeting();
int getBet();
char getSuit(int suit);
char getRank(int rank);
void getFirstHand(int cardRank[], int cardSuit[]);
void getFinalHand(int cardRank[], int cardSuit[], int finalRank[],
                  int finalSuit[], int ranksinHand[],
                  int suitsinHand[]);
int analyzeHand(int ranksinHand[], int suitsinHand[]);

main(void) {
    int bet;
    int bank = 100;
    int i;
    int cardRank[5];
    int cardSuit[5];

    int finalRank[5];
    int finalSuit[5];
    int ranksinHand[13];
    int suitsinHand[4];
    int winnings;
    time_t t;
    char suit, rank, stillPlay;

    //print greeting
    printGreeting();

    // plays a new hand of draw poker

    do {
        bet = getBet();
        srand(time(&t));
        getFirstHand(cardRank, cardSuit);
        printf("Your five cards: \n");

        for (i=0; i<5; i++) {
            suit = getSuit(cardSuit[i]);
            rank = getRank(cardRank[i]);
            printf("Card #%d: %c%c\n", i+1, rank, suit);
        }


        for (i=0; i<4; i++) {
            suitsinHand[i] = 0;
        }

        for (i=0; i<13; i++) {
            ranksinHand[i] = 0;
        }

        getFinalHand(cardRank, cardSuit, finalRank, finalSuit,
                     ranksinHand, suitsinHand);

        printf("Your final five cards: \n");
        for (i = 0; i < 5; i++) {
            suit = getSuit(finalSuit[i]);
            rank = getRank(finalRank[i]);
            printf("Card #%d: %c%c\n", i+1, rank, suit);
        }

        winnings = analyzeHand(ranksinHand, suitsinHand);
        printf("You won %d!\n", bet*winnings);
        bank = bank - bet + (bet*winnings);
        printf("\nYour bank is now %d.\n", bank);
        printf("\nDo you want to play again? ");
        scanf(" %c", &stillPlay);
    } while (toupper(stillPlay) == 'Y');


    return 0;
}

/******************************************************************/

// Print a quick greeting as well as tell the users the value
// of different winning hands

void printGreeting() {

    printf("Here are the rules: \n");
    printf("You start with 100 credits, and you make a bet from ");
    printf("1 to 5 credits. \n");
    printf("You are dealt 5 cards, and then you choose which ");
    printf("cards to keep ");
    printf("or discard.\n");
    printf("You want to make the best possible hand. \n");
    printf("\nHere is the table for winnings (assuming a ");
    printf("bet of 1 credit):");
    printf("\nPair\t\t\t\t1 credit");
    printf("\nTwo Pairs\t\t\t2 credits");
    printf("\nThree Pairs\t\t\t3 credits");
    printf("\nStraight\t\t\t4 credits");
    printf("\nFlush\t\t\t\t5 credits");
    printf("\nFull House\t\t\t8 credits");
    printf("\nFour of a Kind\t\t\t10 credits");
    printf("\nStraight Flush\t\t\t20 credits");
    printf("\n\nHave Fun!!\n\n");
}

// Function to deal the first five cards

void getFirstHand(int cardRank[], int cardSuit[]) {
    int i, j;
    int cardDup;

    for (i=0; i < 5; i++) {
        cardDup = 0;
        do {
            cardRank[i] = (rand() %13);
            cardSuit[i] = (rand() %4);

            for (j=0; j < i; j++) {
                if ((cardRank[i] == cardRank[j]) &&
                    (cardSuit[i] == cardSuit[j])) {
                    cardDup = 1;
                }
            }
        } while (cardDup == 1);
    }
}

// Function that changes the Suit integer value to a character
// representing the suit

char getSuit(int suit) {
    switch (suit) {
    case 0:
        return('c');
    case 1:
        return('d');
    case 2:
        return('h');
    case 3:
        return('s');
    }
}

// Function that changes the rank integer value to a character
// representing the rank

char getRank(int rank) {
    switch (rank) {
    case 0:
        return('A');
    case 1:
        return('2');
    case 2:
        return('3');
    case 3:
        return('4');
    case 4:
        return('5');
    case 5:
        return('6');
    case 6:
        return('7');
    case 7:
        return('8');
    case 8:
        return('9');
    case 9:
        return('T');
    case 10:
        return('J');
    case 11:
        return('Q');
    case 12:
        return('K');
    }
}

// Function to get the user's bet, between 1 and 5

int getBet() {
    int bet;

    do {
        printf("How much do you want to bet? (Enter a number ");
        printf("1 to 5, or 0 to quit the game):");
        scanf(" %d", &bet);

        if (bet >= 1 && bet <= 5) {
            return(bet);
        } else if (bet == 0) {
            exit(1);
        } else {
            printf("\n\nPlease enter a bet from 1-5 or ");
            printf("0 to quit the game. \n");
        }
    } while ((bet < 0) || (bet > 5));
}

// Last function reviews the final hand and determines the value of
// the hand

int analyzeHand(int ranksinHand[], int suitsinHand[]) {
    int num_consec = 0;
    int i, rank, suit;
    int straight = FALSE;
    int flush = FALSE;
    int four = FALSE;
    int three = FALSE;
    int pairs = FALSE;

    for (suit = 0; suit < 4; suit ++)
        if (suitsinHand[suit] == 5)
            flush = TRUE;
    rank = 0;
    while (ranksinHand[rank] == 0)
        rank++;
    for (; rank < 13 && ranksinHand[rank]; rank++)
        num_consec++;
    if (num_consec == 5) {
        straight = TRUE;
    }
    for (rank = 0; rank < 13; rank++) {
        if (ranksinHand[rank] == 4)
            four = TRUE;
        if (ranksinHand[rank] == 3)
            three = TRUE;
        if (ranksinHand[rank] == 2)
            pairs++;
    }

    if (straight && flush) {
        printf("Straight Flush\n\n");
        return(20);
    } else if (four) {
        printf("Four of a Kind\n\n");
        return(10);
    } else if (three && pairs == 1) {
        printf("Full House\n\n");
        return(8);
    } else if (flush) {
        printf("Flush\n\n");
        return(5);
    } else if (straight) {
        printf("Straight\n\n");
        return(4);
    } else if (three) {
        printf("Three of a Kind\n\n");
        return(3);
    } else if (pairs == 2) {
        printf("Two Pairs\n\n");
        return(2);
    } else if (pairs == 1) {
        printf("Pair\n\n");
        return(1);
    } else {
        printf("High Card\n\n");
        return(0);
    }
}

void getFinalHand(int cardRank[], int cardSuit[], int finalRank[],
                  int finalSuit[], int ranksinHand[],
                  int suitsinHand[]) {
    int i, j, cardDup;
    char suit, rank, ans;

    for(i=0; i < 5; i++) {
        suit = getSuit(cardSuit[i]);
        rank = getRank(cardRank[i]);
        printf("Do you want to keep card #%d: %c%c", i+1, rank, suit);
        printf("\nPlease answer (Y/N): ");
        scanf(" %c", ans);
        if (toupper(ans) == 'Y') {
            finalRank[i] = cardRank[i];
            finalSuit[i] = cardSuit[i];
            ranksinHand[finalRank[i]]++;
            suitsinHand[finalSuit[i]]++;
            continue;
        } else if (toupper(ans) == 'N') {
            cardDup = 0;
            do {
                cardDup = 0;
                finalRank[i] = (rand() % 13);
                finalSuit[i] = (rand() % 4);

                for (j=0; j < 5; j++) {
                    if ((finalRank[i] == finalRank[j]) &&
                        (finalSuit[i] == finalSuit[j])) {
                        cardDup = 1;
                    }
                }
            } while (cardDup == 1);
            ranksinHand[finalRank[i]]++;
            suitsinHand[finalSuit[i]]++;
        }
    }
}
#包括
#包括
#包括
#包括
#定义FALSE 0
#定义真1
无效打印问候语();
int getBet();
char getSuit(内部套装);
char getRank(int-rank);
void getFirstHand(int cardRank[],int cardSuit[]);
作废getFinalHand(内部信用卡等级[],内部信用卡套装[],内部最终银行[],
国际金融中心[],国际金融中心[],
int suitsinHand[]);
int-analyzeHand(int-ranksinHand[],int-suitsinHand[]);
主(空){
int-bet;
国际银行=100;
int i;
int cardRank[5];
int cardSuit[5];
国际金融银行[5];
国际金融诉讼[5];
int ranksinHand[13];
int suitsinHand[4];
国际奖金;
时间;
char套装,军衔,剧照;
//打印问候语
打印问候语();
//玩一手新的抽牌扑克
做{
bet=getBet();
srand(time&t));
获取第一手资料(cardRank、cardSuit);
printf(“您的五张卡片:\n”);

对于(i=0;i您忘记了
scanf
中的行号
276
,因此请使用以下方法:

scanf(" %c", &ans);
而不是

scanf(" %c", ans);

请正确设置代码格式。
main(void)
int main(void)
寻求调试帮助的问题(为什么此代码不起作用?)必须包括所需的行为、特定的问题或错误以及在问题本身中重现问题所需的最短代码。没有明确问题陈述的问题对其他读者没有用处。请参阅:如何创建。
scanf(“%c”,ans);
->
scanf(“%c”,ans)
?@O.Hernandez,这就是请求的目的。在创建MCVE的过程中,大多数情况下,您会自己发现实际问题。:)