gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 07:29:09 25 4
gpt4 key购买 nike

我正在为学校解决一个问题,我在 suitsInHand 或 facesInHand 数组的每一行中都得到“未声明的标识符”。上次发生这种情况是因为我没有在 block 的顶部声明我的变量,但据我所知,这不是出于同样的原因而发生的。谁能告诉我我做错了什么?

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


}

最佳答案

这里:

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

suitsInHand 未在此范围内声明。 suitsInHandmain 函数的范围内声明,而不是在 dealACard 范围内声明。

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

这同样适用于 facesInHand

关于c - 未声明的标识符已经声明?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15466725/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com