J.Doe J.Doe - 2 months ago 8
C Question

Why am I getting type specifier error in C programming?

I'm writing a program that shows the daily flights from one city to another:

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


i'm pretty much done with my code but i'm getting errors that I can't seem to figure out why.

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


The code should work but the error i'm betting are type specifier missing, redefinition of departure, array subscript is not an integer, and non-object type char

Answer

First, your compiler should tell you what line each error is on. Look at those lines, and you should be able to figure out how each error should be fixed.

For example, compiling your code with gcc I get an error message:

main.c:16:5: error invalid initializer

The 16 tells me that the error is on line 16 of main.c; precisely, the error occurred 5 characters into line 16 of the file main.c. The invalid initializer is, indeed, one of your departure[x] = y statements. Regardless, I'll try to break down the errors I see in your code snippet. Please let me know if something is unclear.


In your block of code where:

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;

You've already defined d1 through d8 on your third line. So that's at least one of your redefinition errors. Probably remove them from the third line if you want to initialize them globally like that. Also, a bunch of your array assignments use variables that haven't been declared yet (e.g., you probably meant departure[1] = d2; instead of departure[1] = dept2;). Additionally, I've never initialized a global array like that before. I'm not sure that will actually work, though I could very well be wrong. My intuition is to stick your departure[x] statements into some function and call that function as the very first line inside main. Something like:

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

And then inside main:

int main()
{
    initializeArray();

    // the rest of your main function here
}


Inside your main function, you have a couple undeclared variables: index and usertime.

'index' is referred to inside your for-loop in the main function:

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

You need to first declare index as an integer. At least something like

int index;

needs to be stuck in before the for-loop, so that it reads more like this:

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

And then it looks like you probably just need to replace usertime with um.


You're also missing the closing while to your do-while loop. Your main function should read more like:

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


I'm not sure if those are 100% of the errors your compiler is telling you about, but I'm confident that they're at least most of them. You should be left with something more like the following (which did compile for me on gcc; I didn't test it to see if it actually does what you want, it just compiles without errors):

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