未声明的标识符已经声明?

时间:2013-03-17 21:58:12

标签: c visual-studio-2010 undeclared-identifier

我正在为学校解决问题,我在使用suitInHand或facesInHand数组的每一行都获得了“未声明的标识符”。这次发生的最后一次是因为我没有在块的顶部声明我的变量,但据我所知,这不是出于同样的原因。谁能告诉我我做错了什么?

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

#define SUITS 4
#define FACES 13
#define AVAILABLE 0
#define TAKEN 1

void dealACard(char *suits[], char *faces[], int deck[][FACES]);
void dealAHand(char *suits[], char *faces[], int deck[][FACES]);
void analyzeHand(char *suits[], char *faces[]);

int main(void){
    int suitsInHand[4] = {0};
    int facesInHand[13] = {0};
    char *suits[4] = {"Hearts", "Diamonds", "Spades", "Clubs"};
    char *faces[13] = {"Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King", "Ace"};
    int deck[4][13] = {AVAILABLE};

    srand(time(NULL));
    dealAHand(suits, faces, deck);

    system("pause");
    return 0;
}

void dealAHand(char *suits[], char *faces[], int deck[][FACES]){
    int i;
    for(i=0;i<5;i++)
        dealACard(suits,faces,deck);
}

void dealACard(char *suits[], char *faces[], int deck[][FACES]){
    int suitIndex, faceIndex;
    memset(suitsInHand, 0, sizeof(suitsInHand));
    memset(facesInHand, 0, sizeof(facesInHand));

    suitIndex = rand() % 4;
    faceIndex = rand() % 13;
    while (deck[suitIndex][faceIndex]==TAKEN){
        suitIndex = rand() % 4;
        faceIndex = rand() % 13;
    }
    deck[suitIndex][faceIndex]=TAKEN;
    suitsInHand[suitIndex]++;
    facesInHand[faceIndex]++;

    printf("%s of %s.\n", faces[faceIndex], suits[suitIndex]);
}

void analyzeHand(char *suits[], char *faces[]){
    typedef enum {false, true} bool;
    int num_consec = 0;
    int rank, suit;
    bool straight = false;
    bool flush = false;
    bool four = false;
    bool three = false;
    int pairs = 0;

    for(suit=0;suit<SUITS;suit++)
        if(suitsInHand[suit]==5)
            flush = true;

    rank = 0;
    while (facesInHand[rank]==0)
        rank++;

    //for(;rank<FACES && facesInHand[rank]; rank++)
    //  num_consec++;

    if(num_consec == 5){
        straight = true;
        return;
    }

    for(rank = 0;rank<NUM_RANKS; rank++){
        if(facesInHand[rank]==4)
            four = true;
        if(facesInHand[rank]==3)
            three = true;
        if(facesInHand[rank]==2)
            pairs++;
    }

    if(straight == true && flush == true)
        printf("Straight flush.\n\n");
    else if(four == true)
        printf("Four of a kind.\n\n");
    else if(three == true && pairs == 1)
        printf("Full house.\n\n");
    else if(flush == true)
        printf("Flush.\n\n");
    else if(straight == true)
        printf("Straight.\n\n");
    else if(three == true)
        printf("Three of a kind.\n\n");
    else if(pairs == 2)
        printf("Two pairs.\n\n");
    else if(pairs == 1)
        printf("Pair.\n\n");
    else
        printf("High card.\n\n");


}

2 个答案:

答案 0 :(得分:2)

下面:

void dealACard(char *suits[], char *faces[], int deck[][FACES]){
    int suitIndex, faceIndex;
    memset(suitsInHand, 0, sizeof(suitsInHand));

suitsInHand未在此范围内声明。 suitsInHand在[{1}}函数范围内声明main不在dealACard范围内。

要解决此问题,您可以在函数中添加一个参数,以便在文件范围内传递suitsInHand或声明suitsInHand

同样适用于facesInHand

答案 1 :(得分:1)

你在main中声明了suitsInHand,但这并不意味着它可用于其他函数,你需要将它传递给其他函数才能让它们访问它。

你已经到了很多东西,你应该将它们放入一个结构中,然后传递结构。

另外,一般来说,你的dealACard函数可能非常讨厌。特别是如果牌组中只有一张牌,可能需要很长时间才能获得正确的随机数来处理牌组中的最后一张牌