-
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.
-
Why do you have an array named incident and also a struct typedef to incident?
-
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;
}
-
Post the error messages, including the line numbers.
-
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;
}
-
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);
^~
-
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.
-
> 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.