gpt4 book ai didi

c - 为什么我在 C 编程中遇到类型说明符错误?

转载 作者:行者123 更新时间:2023-11-30 20:04:33 27 4
gpt4 key购买 nike

我正在编写一个程序,显示从一个城市到另一个城市的每日航类:

 Departure time:                Arrival Time
8:00 10:16 am
9:43 am 11:52 am
11:19 am 1:31p.m
12:47 pm 3:00pm
2:00pm 4:08 p.m
3:45 p.m 5:55 p.m
7:00pm 9:20p.m
9:45p.m 11:58 p.m
My program asks the user to enter time in 24 hour clock then it displays the departure and arrival times for the flight whole departure time is closest to the the entered by user.
ex: enter a 24-hour time: 13:15
Closest departure time is 12:47 p.m driving at 3.00 pm

我的代码已经完成了,但是我遇到了一些错误,我似乎无法弄清楚为什么。

#include<stdio.h>
#include<math.h>
int d1,d2,d3,d4,d5,d6,d7,d8,hh,mm,um;
int departure[8];
int choice,id;
char m1[80]="Closet Departure time is 8:00 A.M,arriving at 10:16 A.M";
char m2[80]="Closet Departure time is 9:43 A.M,arriving at 11:52 A.M";
char m3[80]="Closet Departure time is 11:19 A.M,arriving at 1:31 P.M";
char m4[80]="Closet Departure time is 12:47 P.M,arriving at 3:00 P.M";
char m5[80]="Closet Departure time is 02:00 P.M,arriving at 4:08 P.M";
char m6[80]="Closet Departure time is 03:45 P.M,arriving at 5:55 P.M";
char m7[80]="Closet Departure time is 07:00 P.M,arriving at 9:20 P.M";
char m8[80]="Closet Departure time is 09:45 P.M,arriving at 11:58 P.M";

int d1=8*60;
departure[0]=d1;

int d2=9*60+43;
departure[1]=dept2;

int d3=11*60+19;
departure[2]=dept3;

int d4=12*60+47;
departure[3]=dept4;

int d5=14*60;
departure[4]=dept5;

int d6=15*60+45;
departure[5]=dept6;

d7=19*60;
departure[6]=dept7;

d8=21*60+45;
departure[7]=dept8;




int main ()
{
do
{

printf("Enter a time in 24-hour format:");
//prompt for time in hh and mm
scanf("%d:%d",&hh,&mm);

//calculate time in minutes
um=hh*60+mm;

int closeDepartureIndex=0;
int closeDeparture=usertime-departure[0];

//find the closest time
for(index=1;index<8;index++)
{
if(abs((um-departure[index]))<closeDeparture)
{
closeDeparture= usertime-departure[index];
closeDepartureIndex=index;
}
}


//pring message of corresponding index
if(closeDepartureIndex==0)
printf("%s",m1);
else if(closeDepartureIndex==1)
printf("%s",m2);
else if(closeDepartureIndex==2)
printf("%s",m3);
else if(closeDepartureIndex==3)
printf("%s",m4);
else if(closeDepartureIndex==4)
printf("%s",m5);
else if(closeDepartureIndex==5)
printf("%s",m6);
else if(closeDepartureIndex==6)
printf("%s",m7);
else if(closeDepartureIndex==7)
printf("%s",m8);

return 0;
}

代码应该可以工作,但我打赌的错误是缺少类型说明符、重新定义出发、数组下标不是整数和非对象类型 char

最佳答案

首先,你的编译器应该告诉你每个错误发生在哪一行。查看这些行,您应该能够弄清楚如何修复每个错误。

例如,使用 gcc 编译代码时我收到一条错误消息:

main.c:16:5: error invalid initializer

16 告诉我错误出现在 main.c 的第 16 行;确切地说,错误发生在文件 main.c 的第 16 行中的 5 个字符。事实上,无效的初始值设定项是出发[x] = y 语句之一。无论如何,我会尝试分解我在您的代码片段中看到的错误。如果有不清楚的地方,请告诉我。


在您的代码块中:

int d1=8*60;
departure[0]=d1;

int d2=9*60+43;
departure[1]=dept2;

int d3=11*60+19;
departure[2]=dept3;

int d4=12*60+47;
departure[3]=dept4;

int d5=14*60;
departure[4]=dept5;

int d6=15*60+45;
departure[5]=dept6;

d7=19*60;
departure[6]=dept7;

d8=21*60+45;
departure[7]=dept8;

您已经在第三行定义了 d1 到 d8。所以这至少是你的重新定义错误之一。如果你想像这样全局初始化它们,可能会从第三行删除它们。此外,大量数组赋值使用尚未声明的变量(例如,您可能意味着 departure[1] = d2; 而不是 departure[1] = dept2;)。此外,我以前从未像这样初始化过全局数组。我不确定这是否真的有效,尽管我很可能是错的。我的直觉是将出发[x]语句粘贴到某个函数中,并将该函数作为 main 中的第一行调用。像这样的东西:

void initializeArray()
{
// departure[x] = y; statements here
}

然后在 main 中:

int main()
{
initializeArray();

// the rest of your main function here
}


在主函数中,有几个未声明的变量:indexusertime

“index”在 main 函数的 for 循环中被引用:

//find the closest time
for(index=1;index<8;index++)
{
if(abs((um-departure[index]))<closeDeparture)
{
closeDeparture= usertime-departure[index];
closeDepartureIndex=index;
}
}

您需要首先将索引声明为整数。至少类似

int index;

需要卡在for循环之前,这样它读起来更像这样:

//find the closest time
int index;
for(index=1;index<8;index++)
{
if(abs((um-departure[index]))<closeDeparture)
{
closeDeparture= usertime-departure[index];
closeDepartureIndex=index;
}
}

然后看起来您可能只需将 usertime 替换为 um


您还缺少 do-while 循环的结束 while 。您的主要功能应该更像是:

int main()
{
do
{
// your main things
} while( /* some condition of your choice */ );
return 0;
}


我不确定这些是否是编译器告诉您的 100% 错误,但我相信它们至少是其中的大部分。您应该留下类似于以下内容的内容(它确实在 gcc 上为我进行了编译;我没有测试它以查看它是否确实符合您的要求,它只是编译时没有错误):

#include<stdio.h>
#include<math.h>
int hh,mm,um;
int departure[8];
int choice,id;
char m1[80]="Closet Departure time is 8:00 A.M,arriving at 10:16 A.M";
char m2[80]="Closet Departure time is 9:43 A.M,arriving at 11:52 A.M";
char m3[80]="Closet Departure time is 11:19 A.M,arriving at 1:31 P.M";
char m4[80]="Closet Departure time is 12:47 P.M,arriving at 3:00 P.M";
char m5[80]="Closet Departure time is 02:00 P.M,arriving at 4:08 P.M";
char m6[80]="Closet Departure time is 03:45 P.M,arriving at 5:55 P.M";
char m7[80]="Closet Departure time is 07:00 P.M,arriving at 9:20 P.M";
char m8[80]="Closet Departure time is 09:45 P.M,arriving at 11:58 P.M";

int d1=8*60;
int d2=9*60+43;
int d3=11*60+19;
int d4=12*60+47;
int d5=14*60;
int d6=15*60+45;
int d7=19*60;
int d8=21*60+45;

void initializeDepartures( )
{
departure[0]=d1;
departure[1]=d2;
departure[2]=d3;
departure[3]=d4;
departure[4]=d5;
departure[5]=d6;
departure[6]=d7;
departure[7]=d8;
}


int main ()
{
initializeDepartures();

do
{

printf("Enter a time in 24-hour format:");
//prompt for time in hh and mm
scanf("%d:%d",&hh,&mm);

//calculate time in minutes
um=hh*60+mm;

int closeDepartureIndex=0;
int closeDeparture=usertime-departure[0];

//find the closest time
int index;
for(index=1;index<8;index++)
{
if(abs((um-departure[index]))<closeDeparture)
{
closeDeparture= usertime-departure[index];
closeDepartureIndex=index;
}
}


//pring message of corresponding index
if(closeDepartureIndex==0)
printf("%s",m1);
else if(closeDepartureIndex==1)
printf("%s",m2);
else if(closeDepartureIndex==2)
printf("%s",m3);
else if(closeDepartureIndex==3)
printf("%s",m4);
else if(closeDepartureIndex==4)
printf("%s",m5);
else if(closeDepartureIndex==5)
printf("%s",m6);
else if(closeDepartureIndex==6)
printf("%s",m7);
else if(closeDepartureIndex==7)
printf("%s",m8);
} while( 1 );

return 0;
}

关于c - 为什么我在 C 编程中遇到类型说明符错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39691370/

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