[英]I'm getting an error that says conflicting types in a C program
我正在处理这项任务,但无法运行。 我尝试了很多不同的事情,例如切换函数isValid的位置,但是当我在函数getGuess之前声明它时,代码会在函数populateColorArray被调用后立即停止并关闭程序。
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#define COLORS 8
#define THREE 3
#define FOUR 4
#define TEN 10
#define TRUE 1
#define FALSE 0
enum color {BLACK, GREEN, NAVY, ORANGE, PINK, RED, VIOLET, WHITE};
int main()
{
srand((time(0)));
int i;
int j;
char colors[COLORS] = {'B', 'G', 'N', 'O', 'P', 'R', 'V', 'W'};
int secretCode[FOUR];
int guesses[TEN][FOUR];
int clues[TEN][FOUR];
welcomeScreen();
clearScreen();
setCode(secretCode);
printf("\n");
clearScreen();
displayBoard();
populateColorArray(colors);
initializeArray(guesses);
getGuess(guesses, colors);
for(i = 0; i < TEN; i++)
{
for(j = 0; j < FOUR; j++)
{
printf("%c", guesses[i][j]);
}
}
return 0;
}
void welcomeScreen()
{
printf(" ###################################################################\n");
printf(" ###################################################################\n");
printf(" ######################## #####################\n");
printf(" ######################## Mastermind #####################\n");
printf(" ######################## #####################\n");
printf(" ###################################################################\n");
printf(" ###################################################################\n");
printf("Rules:\n");
printf("1. The CodeMaker sets a secret code\n");
printf("2. The CodeBreaker tries to match the code using logic and deduction\n");
printf("3. After each move, the CodeMaker gives clues to the CodeBreaker\n");
printf("4. The CodeBreaker has 10 attempts to guess the secret code\n");
printf("\n");
}
// clears the screen for the next step of the program
void clearScreen()
{
printf(" #########################\n");
printf("\n");
printf(" <Hit enter to continue>\n");
printf("\n");
printf(" #########################\n");
char key;
scanf("%c", &key);
system("cls");
}
// board of the game
void displayBoard()
{
printf("+---------------------------------------+\n");
printf("| SECRET CODE |\n");
printf("+---------------------------------------+\n");
printf("| ? ? ? ? |\n");
printf("+---------------------------------------+\n");
printf("| PLAYER GUESS | CLUES |\n");
printf("+---------------------------------------+\n");
printf("| ? ? ? ? | ? ? ? ? |\n");
printf("+---------------------------------------+\n");
printf("| ? ? ? ? | ? ? ? ? |\n");
printf("+---------------------------------------+\n");
printf("| ? ? ? ? | ? ? ? ? |\n");
printf("+---------------------------------------+\n");
printf("| ? ? ? ? | ? ? ? ? |\n");
printf("+---------------------------------------+\n");
printf("| ? ? ? ? | ? ? ? ? |\n");
printf("+---------------------------------------+\n");
printf("| ? ? ? ? | ? ? ? ? |\n");
printf("+---------------------------------------+\n");
printf("| ? ? ? ? | ? ? ? ? |\n");
printf("+---------------------------------------+\n");
printf("| ? ? ? ? | ? ? ? ? |\n");
printf("+---------------------------------------+\n");
printf("| ? ? ? ? | ? ? ? ? |\n");
printf("+---------------------------------------+\n");
printf("| ? ? ? ? | ? ? ? ? |\n");
printf("+---------------------------------------+\n");
}
//function has as an argument codeArray[] that later on it's used when calling the function convertColor
void setCode (int codeArray[])
{
int i;
printf("Integer Secret Code\n");
for(i = 0; i < 4; ++i)
{
codeArray[i] = getColor();
printf("%d ", codeArray[i]);
}
printf("\n");
printf("Color Secret Code\n");
for(i = 0; i < 4; ++i)
{
convertColor(codeArray[i]);
}
}
// function used to get the random colors
int getColor ()
{
int colorRandom = rand() %COLORS;
return colorRandom;
}
// function used to convert the numbers into an actual color string
void convertColor (int color)
{
switch (color)
{
case 0:
printf("Black ");
break;
case 1:
printf("Green ");
break;
case 2:
printf("Navy ");
break;
case 3:
printf("Orange ");
break;
case 4:
printf("Pink ");
break;
case 5:
printf("Red ");
break;
case 6:
printf("Violet ");
break;
case 7:
printf("White ");
break;
}
}
// function that receives the array color[] and prints the char equivalent to each color
void populateColorArray(char color[])
{
int i;
printf("\nCharacter colors are:\n");
for(i = 0; i < COLORS; ++i)
{
printf("%c ", color[i]);
}
printf("\n");
}
// used to set all row and columns of the array as -1
void initializeArray(int guesses[TEN][FOUR])
{
int i;
int j;
for (i = 0; i < TEN; ++i)
{
for (j = 0; j < FOUR; ++j)
{
guesses[i][j] = -1;
}
}
}
/* function that is going to use isValid function to check if the user's
guess is valid and continue the game from there*/
void getGuess(int guesses[TEN][FOUR], char colors[])
{
static int row = 0;
int col = 0;
int valid = FALSE;
char guess[TEN];
int i;
while(valid = FALSE)
{
printf("Enter your guess of four colors (no spaces):\n");
for(i = 0; i < FOUR; ++i)
{
scanf("%c", &guess[i]);
}
printf("You entered: ");
for(i = 0; i < FOUR; ++i)
{
printf("%c", guess[i]);
}
if(strlen(guess[TEN]) == 4)
{
for (i = 0; i < FOUR; ++i)
{
guess[i] = toupper(guess[i]);
if (isalpha(guess[i]) == TRUE)
{
if(isValid(colors, guess[i]) == TRUE)
{
row = guess[i];
col = guess[i];
if(col == THREE)
{
valid = TRUE;
}
}
else
{
printf("The value you entered is invalid");
valid = FALSE;
break;
}
}
else
{
printf("Invalid value entered %c, try again", guess[i]);
valid = FALSE;
break;
}
}
}
else
{
printf("Incorrect number of letters entered");
valid = FALSE;
}
}
row = row + 1;
}
// actually validates the user guess
// giving an error "conflicting types for 'isValid'
int isValid(char colors[COLORS], char color)
{
int c;
int valid = FALSE;
for(c = 0; c < COLORS; ++c)
{
if(colors[c] == color)
{
return TRUE;
}
}
return FALSE;
}
如果有人知道我还能做些什么来让它运行,我将不胜感激。
在这里,您将char
传递给strlen
而不是char*
:
if(strlen(guess[TEN]) == 4)
结果有未定义的行为。 char
将被转换为char*
并从该内存地址开始读取。 不仅如此, TEN
是出界的。 您可以在guess
使用的最高索引是TEN - 1
。 它应该是这样的:
if(strlen(guess) == FOUR)
在这里,您将FALSE
分配给valid
而不是检查值:
while(valid = FALSE)
它应该是:
while(valid == FALSE)
当你从用户那里读取颜色时,你是一个字符一个字符地做的,所以guess
不会以空字符结尾:
for(i = 0; i < FOUR; ++i)
{
scanf("%c", &guess[i]);
}
把它变成这样的:
if(scanf("%4s", guess) != 1) {
printf("try again\n");
continue;
}
当您使用函数isalpha
您假设如果char
是字母,它将返回1
( TRUE
)。 这种假设是错误的。 如果是字母,它将返回非零值。
if(isalpha(guess[i]) == TRUE)
应该
if(isalpha((unsigned char) guess[i]))
在这里,您使用参数guesses
,但在函数中根本没有使用它,这是非常可疑的:
void getGuess(int guesses[TEN][FOUR], char colors[])
另外:您以错误的顺序定义了函数。 重新排序它们,以便在使用它们之前声明它们:
void welcomeScreen() { ... }
void clearScreen() { ... }
void displayBoard() { ... }
int getColor() { .. }
void convertColor(int color) { ... }
void setCode(int codeArray[]) { ... }
void populateColorArray(char color[]) { ... }
void initializeArray(int guesses[TEN][FOUR]) { ... }
int isValid(char colors[COLORS], char color) { ... }
void getGuess(int guesses[TEN][FOUR], char colors[]) { ... }
int main() { ... }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.