gpt4 book ai didi

编译错误 'incompatible pointer to integer conversion passing ' int (int, int )' to parameter of type ' int' [-Wint-conversion]"

转载 作者:太空宇宙 更新时间:2023-11-04 07:52:38 41 4
gpt4 key购买 nike

此代码采用用户输入(字符 C、T、B)和(int 0-24 和 0-60)来计算 parking 成本关于用户输入的车辆类型。

我怀疑错误发生在函数 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;
}

最佳答案

main 中你做:

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

main 中没有名为 total_hour_parked 的变量,因此编译器认为您正在尝试将函数指针传递给 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')

关于编译错误 'incompatible pointer to integer conversion passing ' int (int, int )' to parameter of type ' int' [-Wint-conversion]",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52827714/

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