编译错误'指向整数转换的不兼容指针,该指针将'int(int,int)'传递给'int'类型的参数[-Wint-conversion]“

时间:2018-10-16 03:50:17

标签: c

此代码从用户(字符C,T,B )和( int 0-24和0-60 )输入,以计算基于停车的费用用户输入的车辆类型。

我怀疑函数charged中发生错误,因此,我无法在收到此错误的代码的最后一行中打印结果


[cquery]指向整数转换的不兼容指针,将'int(int,int)'传递给'int'类型的参数[-Wint-conversion]


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



int total_minute_parked (int minute_in, int minute_left)
{
  int minute_parked;
  if (minute_in > minute_left)
    {
      minute_parked = (minute_left  - minute_in + 60);
    }
    else 
      {
        minute_parked = (minute_left - minute_in);
      }
 return minute_parked;     
}

// func calc total hours parked 

int total_hour_parked (int hour_in, int hour_left)
{
  int hours_parked;
  if (hour_left > hour_in)
    {

      hours_parked = abs((hour_left - 1) - hour_in);
    }  
    else 
      {
        hours_parked = abs(hour_left - hour_in);
      }

return hours_parked ;
}

// -------------------funtion to calc charge based off type of vehicle------ 

float charged (char vehicle_type,int total_hour_parked)
{

char C;
char T;
char B;

float temp_charged;

if  (vehicle_type == C) // -------------------------------CAR 
  {
    if (total_hour_parked > 3) 
    {
      float secondary_hour = total_hour_parked - 3;
      temp_charged = secondary_hour * 1.5;
    }
    else 
        {
          temp_charged = 0;
        }
  } 

else if  (vehicle_type == T)   // ------------------------------TRUCK 
  {
    if (total_hour_parked > 2) 
    {
      float secondary_hour = total_hour_parked - 2;
      temp_charged = (secondary_hour * 2.3) + 1.0;
    }
    else {
          temp_charged = 1;
        }
  } 

else if  (vehicle_type == B) // -----------------------------------BUS 
  {
    if (total_hour_parked > 1) 
    {
      float secondary_hour = total_hour_parked - 1;
      temp_charged = (secondary_hour * 3.7) + 2.0;
    }
    else {
          temp_charged = 2;
        }
  } 
return temp_charged;
}

//---------------------- end program upon invalid imput -------------------// 



// --------------------- main that prints results and takes imput -----------//

int main() 

{

  int total_hour_parked (int hour_in,int hour_left);
  float charged (char vehicle_type, int total_hour_parked);
char vehicle_type;
int hour_in = 0;
int minute_in = 0;

int hour_left = 0;
int minute_left = 0;

printf("Please enter the type of Vehicle:"); scanf("%c",&vehicle_type); 

printf("Please enter the hour entered lot:"); scanf("%d", &hour_in);

printf("Please enter the minute entered lot:"); scanf("%d", &minute_in);

printf("Please enter the hour left lot:"); scanf("%d", &hour_left);

printf("Please enter the minute left lot:"); scanf("%d", &minute_left);

printf("------------------------------------\n");

printf("Parking time: %d:%d\n", total_hour_parked(hour_in,hour_left),total_minute_parked(minute_in,minute_left));

printf("Cost %f",charged(vehicle_type,total_hour_parked));

  return 0;
}

1 个答案:

答案 0 :(得分:2)

main中,您执行以下操作:

printf("Cost %f",charged(vehicle_type,total_hour_parked));

但是total_hour_parked中没有名为main的变量,因此编译器认为您正在尝试将函数指针传递给 function total_hour_parked

由于函数charged希望将整数作为第二个参数,因此您会收到如下消息:

expected ‘int’ but argument is of type ‘int (*)(int,  int)’
         ^^^^^                          ^^^^^^^^^^^^^^^^^^
        expected                         actual (i.e. function pointer)

也许您想做:

printf("Cost %f",charged(vehicle_type,total_hour_parked(hour_in,hour_left)));

通常,应避免使用相同的命名变量和函数,因为这会引起混淆。

另一个问题是这些类型的行:

if  (vehicle_type == C)

似乎您想检查vehicle_type是否是字符C,因此您应该这样做:

if  (vehicle_type == 'C')