JP12
JP12

Reputation: 1

How do I fix the error conflicting types? c programming

I am attempting to create a program that asks me questions but with different numbers each time so that i can practice for a school competition. This is just a simple example for one of the questions but each time I try to compile the program I get the error: conflicting types where the random float is passed to a function. I'm new at programming and don't see what went wrong.

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

Here are my errors:

 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') 

I had no trouble doing this with ints but I can't get floats to work.

Upvotes: 0

Views: 4760

Answers (2)

nnn
nnn

Reputation: 4220

You don't have the return type for your functions bodies, so they are defaulting to int conflicting with your prototype return type float.

Change for example

question1( )

to

float question1()

and so on.

Also add the parameters passed to the functions in the function prototypes:

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

or (not necessarily needing to specify the parameter name):

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

Upvotes: 1

Buddy
Buddy

Reputation: 11028

Change .1 to .1f and .01 to .01f. That makes the numeric literal a float instead of a double.

Upvotes: 0

Related Questions