简体   繁体   中英

undeclared identifiers already declared?

I'm working on a problem for school and I'm getting "undeclared identifier" in every line with my suitsInHand or facesInHand arrays. The last time this happened was because I wasn't declaring my variables at the top of the block, but as far as I can tell this isn't happening for the same reason. Can anyone tell me what I've done wrong?

#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");


}

Here:

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

suitsInHand is not declared in this scope. suitsInHand is declared in the scope of main function not in dealACard scope.

To fix this, you can add a parameter to your functions to pass suitsInHand or declare suitsInHand at file scope.

The same applies for facesInHand .

youve declared suitsInHand in main, but that doesnt mean its available to other functions, you need to pass it to other functions for them to have access to it.

you are getting to the point where you are passing quite a few things around that you should put them into a struct and then pass the struct around.

Also, in general, your dealACard function is potentially very very nasty. Especially if there is only 1 card in the deck, it might take a very long time to get the right random number to deal the last card in the deck

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM