c - 如何修复类型冲突的错误? c 编程

标签 c error-handling compiler-errors conflicting-libraries

我正在尝试创建一个程序,每次都会问我不同的问题,以便我可以为学校比赛进行练习。这只是其中一个问题的一个简单示例,但每次我尝试编译程序时都会收到错误:随机 float 传递给函数的类型冲突。我是编程新手,不知道出了什么问题。

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

float fixran1();
float fixran2();
int ovrall=0;
float question1();

int main()
{
    question1();
}

question1( )
{
    float a, b;
    float ran11=fixran1((((float)rand()/(float)(RAND_MAX))*50)+25);
    float ran12=fixran2((((float)rand()/(float)(RAND_MAX))*15)+7);
    char ques1[]="If an employee works %f hours and is paid $%f per hour, how much is their gross pay?";

printf(ques1, ran11, ran12);
scanf("%f",a);
b=ran11*ran12;

if(a==b)
{
    printf("correct");
    ovrall=ovrall+1;
}
else
    printf("incorrect, the right answer is %f", b);
}





fixran1(float ran11)
{
    float i=ran11%.1;
    ran11=ran11-i;
    return(ran11);
}

fixran2(float ran12)
{
    float i=ran12%.01;
    ran12=ran12-i;
    return(ran12);
}

这是我的错误:

 error: conflicting types for 'fixran1'
 error: invalid operands to binary % (have 'float' and 'double')
 error: conflicting types for 'fixran2'
 error: invalid operands to binary % (have 'float' and 'double') 

我在使用整数时没有遇到任何问题,但我无法让 float 工作。

最佳答案

您的函数体没有返回类型,因此它们默认为 int ,与您的原型(prototype)返回类型 float 冲突。

更改示例

question1( )

float question1()

等等。

同时在函数原型(prototype)中添加传递给函数的参数:

float fixran1(float ran11);
float fixran2(float ran12);

或者(不一定需要指定参数名称):

float fixran1(float);
float fixran2(float);

关于c - 如何修复类型冲突的错误? c 编程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33683201/

相关文章:

c - 方括号在C中的数组初始化中意味着什么?

c - 在 Linux 上使用 `splice`……其他系统还有什么?

c++ - 在C++和OpenCV中调用其他文件中的函数

MySQL 存储过程错误处理

android - ActionBarSherlock 4.2 的构建问题

c++ - PRNG库支持存储/恢复排序状态?

php - 如何在 PHP 中捕获 Stripe fatal error ?

php - Zend 框架 - PHP fatal error 的错误页面

java - GCJ 抛出错误 : "Undefined reference to main" when compiling

java - 打印多维数组(Java)的元素时出错