Pregunta

I can't seem to figure out this error in my printf statements..Whenever I change the format specifier from integer to float and vice versa, I just get the same error.

     error: format â%fâ expects type âdoubleâ, but argument 2 has type âdouble (*)(int,     int)â

Here is that part in my code

void outputScores(int x, int y)
{
if(((x&y)>=1) && ((x&y)<=20))
{
printf("------------------------------\n");
printf("\n");
printf("Field: %d,%d\n",x,y);
printf("\n");
printf("Soil quality: %f\n",soilQuality);
printf("Sun exposure: %d\n",sunExposure);
printf("Irrigation exposure: %d\n",irrigationExposure);
printf("\n");
printf("Estimated yield: %d\n",estimatedYield);
printf("Estimated quality: %d\n",estimatedQuality);
printf("Time to harvest: %d\n",harvestTime);
printf("\n");
printf("Overall Score: %f\n",fieldScore);
printf("\n");
printf("------------------------------\n");
}
else
{
printf("Field %d, %d is invalid!\n",x,y);
}
return;
}

and here is the function

 double soilQuality(int x, int y)
 {
 if((x>=1) && (x<=20) && (y>=1) && (y<=20))
 {
 if((x+y)%2==1)
 {
 int soilQuality=(1+(sqrt((x-10)*(x-10))+((y-10)*(y-10))));
 return soilQuality;
 }
 else
 {
 int soilQuality=(1+((abs(x-10)+abs(y-10))/2));
 return soilQuality;
 }
 }
 else
 {
 return -1;
 }
 }
¿Fue útil?

Solución

In printf(), %f expect a double, but you give it a pointer to a function of type double (*)(int, int).

So change

printf("Soil quality: %f\n",soilQuality);

to

printf("Soil quality: %f\n",soilQuality(x, y));

and try again.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top