gpt4 book ai didi

c - boolean 函数错误

转载 作者:太空宇宙 更新时间:2023-11-04 00:47:39 24 4
gpt4 key购买 nike

我是一名新的 C 开发人员(我习惯用 Java 编程),并尝试过创建,我认为是一个简单的 bool 函数。虽然我收到一个我不明白如何修复的错误:

#include <stdio.h>
#include <stdlib.h>
typedef enum { false, true } bool;
int main() {
int currentNumber, round = 1;
printf("Numbers generated will be between 1 and 20. \n");
currentNumber = rand() % 20;
bool validNumber = false;
do {
if(currentNumber != 0) {
validNumber == true;
} else {
currentNumber = rand() % 20;
}
}while(validNumber == false);
printf("You're on round" + ("%d", round));
printf("You're current number is: " + ("%d", currentNumber));
printf("Higher or Lower (H/L)?");
char userInput [20];
scanf("%s", &userInput);
if((userInput[0] == 'h') || (userInput[0] == 'H')) {
completeRound(round, 'H', currentNumber);
} else if((userInput[0] == 'l') || (userInput[0] == 'L')) {
completeRound(round, 'L', currentNumber);
}
}

void completeRound(int round, char input, int currentNumber) {
int initialVal = currentNumber, newVal;
if(input == 'H') {
newVal = rand() % 20;
bool checkResult(initialVal, newVal, input);
} else {
newVal = rand() % 20;
bool checkResult(initialVal, newVal, input);
}
}

bool checkResult(int initialVal, int finalVal, char input);
bool checkResult(int initialVal, int finalVal, char input) {
if(input == 'H') {
if(initialVal <= finalVal) {
return true;
} else {
return false;
}
}
if(input == 'L') {
if(initialVal >= finalVal) {
return true;
}else {
return false;
}
}
printf("An error has occurred! Aborting game...");
return false;
}

错误如下:

\main.c|39|error: conflicting types for 'checkResult'

起初,我认为出于某种原因,在 C 中你只能将某些数据类型作为参数传递给 bool 方法,尽管我在 Google 上找不到直接的答案。除此之外;我不明白“冲突类型”是什么意思(这是我第一次调试 C 程序。

我用来调用checkResult的函数如下:

最佳答案

在调用函数之前,您还需要编写其原型(prototype)。默认情况下,编译器将其视为 int 的返回类型,但实际上它是 bool

所以在调用 checkResult 之前写 bool checkResult(int initialVal, int finalVal, char input)

关于c - boolean 函数错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31534662/

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