Thread: A problem with my code.

  1. #1
    Registered User
    Join Date
    Apr 2020
    Posts
    58

    A problem with my code.

    I have a problem with my code and I can't find a way to solve it and continue adding in it. When I compile it it says that I don't have declared the *event. I am very confused. Here is the code.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    #include <string.h>
    typedef struct
    {
        int year;
        int month;
        int day;
    }date;
    typedef struct
    {
        double latitude;
        double longitude;
    }location;
    typedef struct incList
    {
        char area[100];
        date reported;
        int total_missing;
        int dead_women;
        int dead_men;
        int dead_kids;
        char cause_of_death[150];
        char location_description[500];
        location coordinates;
        char URL[100];
        struct incList *next;
    }incident;
    int main(int argc,char *argv[])
    {
        int enter,number_of_incidents;
        char end[5],token[]=";",buff[BUFSIZ],incident[2500];
        incident *event;
        do
        {
            do
            {
                printf("Give the number of the source from where you want the program to get the data:\n\n1. From the file\n2. From the keyboard\n");
                scanf("%d",&enter);
            }
            while((enter!=1)&&(enter!=2));
            do
            {
                printf("How many incidents do you want to add to the list?\n");
                scanf("%d",&number_of_incidents);
            }
            while(number_of_incidents<=1);
            strcpy(buff,argv);
            while (fgets(buff,BUFSIZ,in))
            {
                event=(incident * )malloc(number_of_incidents*sizeof(incident));
                char *token = strtok(buff,";");
                strcpy(event->area, token );
                token=strtok(NULL,";");
                sscanf(token,"%d/%d/%d",&event->reported->day, &event->reported->month, &event->reported->year);
                token=strtok(NULL,";");
                event->total_missing=strtol(token,NULL,0);
                token=strtok(NULL,";");
                event->dead_women=strtol(token,NULL,0);
                token=strtok(NULL,";");
                event->dead_men= strtol(token,NULL,0);
                token=strtok(NULL,";");
                event->dead_kids=strtol(token,NULL,0);
                token=strtok(NULL,";");
                strcpy(event->cause_of_death,token);
                token=strtok(NULL,";");
                strcpy(event->location_description,token);
                token=strtok(NULL,";");
                sscanf(token,"%.12d",&event->location->latitute);
                token=strtok(NULL,";");
                sscanf(token,"%.12d",&event->location->longitude);
                token=strtok(NULL,";");
                strcpy(event->URL,token);
                token=strtok(NULL,";");
                free(event);
            }
            printf("Do you want to continue adding or exit?[yes/exit]\n");
            scanf("%s",end);
        }
        while(strcmp(end,"exit")!=0);
        return 0;
    }
    Thanks in advance.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,920
    Why do you have an array named incident and also a struct typedef to incident?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Apr 2020
    Posts
    58
    Just fixed that immediately after your reply. But I have problem with the -> in the code. The compiler says that there are errors.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    #include <string.h>
    typedef struct
    {
        int year;
        int month;
        int day;
    }date;
    typedef struct
    {
        double latitude;
        double longitude;
    }location;
    typedef struct incList
    {
        char area[100];
        date reported;
        int total_missing;
        int dead_women;
        int dead_men;
        int dead_kids;
        char cause_of_death[150];
        char location_description[500];
        location coordinates;
        char URL[100];
        struct incList *next;
    }incident;
    int main(int argc,char *argv[])
    {
        int enter,number_of_incidents;
        char end[5],token[]=";",buff[2500];
        incident *event;
        FILE *myStream;
        do
        {
            do
            {
                printf("Give the number of the source from where you want the program to get the data:\n\n1. From the file\n2. From the keyboard\n");
                scanf("%d",&enter);
            }
            while((enter!=1)&&(enter!=2));
            do
            {
                printf("How many incidents do you want to add to the list?\n");
                scanf("%d",&number_of_incidents);
            }
            while(number_of_incidents<=1);
            strcpy(buff,argv);
            while (fgets(buff,2500,myStream))
            {
                event=(incident * )malloc(number_of_incidents*sizeof(incident));
                char *token = strtok(buff,";");
                strcpy(event->area, token );
                token=strtok(NULL,";");
                sscanf(token,"%d/%d/%d",&event->reported->day, &event->reported->month, &event->reported->year);
                token=strtok(NULL,";");
                event->total_missing=strtol(token,NULL,0);
                token=strtok(NULL,";");
                event->dead_women=strtol(token,NULL,0);
                token=strtok(NULL,";");
                event->dead_men= strtol(token,NULL,0);
                token=strtok(NULL,";");
                event->dead_kids=strtol(token,NULL,0);
                token=strtok(NULL,";");
                strcpy(event->cause_of_death,token);
                token=strtok(NULL,";");
                strcpy(event->location_description,token);
                token=strtok(NULL,";");
                sscanf(token,"%.12d",&event->location->latitute);
                token=strtok(NULL,";");
                sscanf(token,"%.12d",&event->location->longitude);
                token=strtok(NULL,";");
                strcpy(event->URL,token);
                token=strtok(NULL,";");
                free(event);
            }
            printf("Do you want to continue adding or exit?[yes/exit]\n");
            scanf("%s",end);
        }
        while(strcmp(end,"exit")!=0);
        fclose(myStream);
        return 0;
    }

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,920
    Post the error messages, including the line numbers.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,168
    A development process
    You need to stop trying to do everything in main, or indeed any function.

    There is absolutely NO reason at all that your parsing code should be anywhere near malloc.
    You're just massively confusing things.

    Break up the code into functions. Each function does ONE thing.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    #include <string.h>
    
    typedef struct
    {
        int year;
        int month;
        int day;
    }date;
    
    typedef struct
    {
        double latitude;
        double longitude;
    }location;
    
    typedef struct incList
    {
        char area[100];
        date reported;
        int total_missing;
        int dead_women;
        int dead_men;
        int dead_kids;
        char cause_of_death[150];
        char location_description[500];
        location coordinates;
        char URL[100];
        struct incList *next;
    } incident;
    
    incident extract_incident(char *buff) {
      incident event;
      char *token = strtok(buff,";");
      strcpy(event.area, token );
      token=strtok(NULL,";");
      sscanf(token,"%d/%d/%d",&event.reported.day, &event.reported.month, &event.reported.year);
      token=strtok(NULL,";");
      event.total_missing=strtol(token,NULL,0);
      token=strtok(NULL,";");
      event.dead_women=strtol(token,NULL,0);
      token=strtok(NULL,";");
      event.dead_men= strtol(token,NULL,0);
      token=strtok(NULL,";");
      event.dead_kids=strtol(token,NULL,0);
      token=strtok(NULL,";");
      strcpy(event.cause_of_death,token);
      token=strtok(NULL,";");
      strcpy(event.location_description,token);
      token=strtok(NULL,";");
      sscanf(token,"%lf",&event.coordinates.latitude);
      token=strtok(NULL,";");
      sscanf(token,"%lf",&event.coordinates.longitude);
      token=strtok(NULL,";");
      strcpy(event.URL,token);
      token=strtok(NULL,";");
      return event;
    }
    
    void read_from_source(FILE *source) {
      char buff[2500];
      while (fgets(buff,2500,source))
      {
        incident event = extract_incident(buff);
        // now you can malloc, save it somewhere more permanent.
      }
    }
    
    
    int main(int argc,char *argv[])
    {
        int enter,number_of_incidents;
        char end[5],token[]=";",buff[2500];
        FILE *myStream;
        do
        {
            do
            {
                printf("Give the number of the source from where you want the program to get the data:\n\n1. From the file\n2. From the keyboard\n");
                scanf("%d",&enter);
            }
            while((enter!=1)&&(enter!=2));
            do
            {
                printf("How many incidents do you want to add to the list?\n");
                scanf("%d",&number_of_incidents);
            }
            while(number_of_incidents<=1);
    
            if(enter == 1) {
              // open file
              read_from_source(myStream);
              fclose(myStream);
            } else if ( enter == 2 ) {
              read_from_source(stdin);
            }
            printf("Do you want to continue adding or exit?[yes/exit]\n");
            scanf("%s",end);
        }
        while(strcmp(end,"exit")!=0);
    
        return 0;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Apr 2020
    Posts
    58
    Those are the errors the compiler pops up in the terminal.

    Code:
    MardikisMavraganis.c:66:44: error: invalid type argument of ‘->’ (have ‘date {aka struct <anonymous>}’)
        sscanf(token,"%d/%d/%d",&event->reported->day, &event->reported->month, &event->reported->year);
                                                ^~
    MardikisMavraganis.c:66:67: error: invalid type argument of ‘->’ (have ‘date {aka struct <anonymous>}’)
        sscanf(token,"%d/%d/%d",&event->reported->day, &event->reported->month, &event->reported->year);
                                                                       ^~
    MardikisMavraganis.c:66:92: error: invalid type argument of ‘->’ (have ‘date {aka struct <anonymous>}’)
     %d/%d",&event->reported->day, &event->reported->month, &event->reported->year);
                                                                            ^~
    MardikisMavraganis.c:80:31: error: ‘incident {aka struct incList}’ has no member named ‘location’
        sscanf(token,"%.12d",&event->location->latitute);
                                   ^~
    MardikisMavraganis.c:82:31: error: ‘incident {aka struct incList}’ has no member named ‘location’
        sscanf(token,"%.12d",&event->location->longitude);
                                   ^~

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,920
    What do you think the error messages are trying to tell you?

    Salem is right though: you need to break up your code into functions. Not only that, but you need to start simple: don't jump into trying to work with a struct with so many members, including members that are also structs. Start with something simpler and build up your confidence in fixing errors as you encounter them.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,168
    > sscanf(token,"%d/%d/%d",&event->reported->day, &event->reported->month, &event->reported->year);
    Compare with what I wrote.

    sscanf(token,"%d/%d/%d",&event.reported.day, &event.reported.month, &event.reported.year);

    Your 'event' might be a pointer, but that doesn't mean you go all -> crazy down every single level.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code problem or compiler problem....?
    By miloki in forum C Programming
    Replies: 4
    Last Post: 03-05-2015, 12:48 AM
  2. Same problem, different code
    By SOS MAR in forum C++ Programming
    Replies: 2
    Last Post: 04-18-2011, 11:41 AM
  3. HELP I need to do this problem, can anyone help with the code
    By AcuteApathy in forum C++ Programming
    Replies: 8
    Last Post: 03-30-2011, 04:21 PM
  4. problem in this code?
    By wise_ron in forum C Programming
    Replies: 9
    Last Post: 10-09-2006, 12:08 PM
  5. Problem with code or Dev-C++ IDE???
    By dbyte in forum C Programming
    Replies: 2
    Last Post: 07-12-2003, 08:38 AM

Tags for this Thread