I am working on this program that takes in a file and stores the information then will break it down and print based off what the user wants. but when I compile I receive no error. but when I execute I receive a Segmentation fault (core dumped) I've really looked into my code but I do not think I understand enough about C to understand the problem. Please let me know what you think.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define SIZE 15
#define Line_size 100
typedef struct {
//parts of the struct
int course_number;
char course_name[Line_size];
int seat_number;
char day[Line_size];
char teacher[Line_size];
char time[Line_size];
} course_t;
//declare other functions
void store(course_t courses[]);
void seperate(course_t courses[]);
void print(course_t courses);
int main(void){
course_t courses[SIZE];
store(courses);
seperate(courses);
return(0);
}
void store(course_t courses[]){
FILE *file = fopen("classes.csv", "r");
//buf holds each line
char buf[Line_size];
//loop over every line and store info
for (int i = 0; i <SIZE; i++){
fgets(buf, Line_size, file);
//printf("%s", buf);
//get the class num
strtok(buf, " ");
char *class_num = strtok(NULL, ",");
courses[i].course_number = atoi(class_num);
//get class name
char *class_name = strtok(NULL, ",");
strcpy(courses[i].course_name, class_name);
char *s_num = strtok(NULL, ",");
s_num = strtok(NULL, ",");
courses[i].seat_number = atoi(s_num);
char *teach = strtok(NULL, "'");
teach = strtok(NULL, "'");
teach = strtok(NULL, "'");
strcpy(courses[i].teacher, teach);
char *d = strtok(NULL, " ");
strcpy(courses[i].day, d);
char *tim = strtok(NULL, " ");
strcpy(courses[i].time, tim);
}
fclose(file);
}
void seperate(course_t courses[]){
int choice;
//printf("working so far?");
do{
//prints list of options to choose from
printf("Choices:\n1 - Print all available classes\n2 - print class given course id\n3 - print all classes given day of week co
mbo\n4 - print all classes under certain # of seats\n5 - prints all classes you would be interested in taking if you want a CS deg
ree\n6 -quit\n");
//the process of printing all of the statements starts here
//they will be sorted by an if else statement and then the results will print and scan depending on the users choice
scanf("%d", &choice);
if (choice == 6){
printf("You exited the program\n");
}
else if (choice == 1){
//to find the number of chairs in a class
}
else if (choice == 2){
char *temp = NULL;
printf("Enter class id:");
scanf("%s", temp);
}
else if (choice == 3){
char *dow = NULL;
//printf("%s", day1);
printf("Enter a day of week combination ");
scanf("%s", dow);
}
else if (choice ==4){
int seat = 0;
printf("Enter maximum number of seat: ");
scanf("%d", &seat);
for(int i = 0; i < SIZE; i++){
if (courses[i].seat_number >= seat){
print(courses[i]);
}else{
printf("num");
}
}
}
else if (choice ==5){
//to find the number of chairs in a class
}
} while (choice != 6);
}
void print(course_t course){
printf("%-35d %-12s %d\n%-5s %-5s %-5s\n", course.course_number, course.course_name, course.seat_number, course.teacher, cours
e.day, course.time);
}
EDIT
upon request here is the input data.
CSCI 107,Joy and Beauty of Computing,33930,62,0,62,Paxton John T,MWF 1000-1050
CSCI 112,Programming with C I,31368,175,0,175,Cummings Mary A,MWF 0900-0950
CSCI 127,Joy and Beauty of Data,33832,155,0,155,DeFrance Daniel Louis,MWF 0900-0950
CSCI 132,Bsc Data Structures/Algorithms,30271,156,0,156,DeFrance Daniel Louis,MWF 1510-1600
CSCI 215,Social & Ethical Issues in CS,33931,60,0,60,Cummings Mary A,TR 1505-1555
CSCI 232,Data Structures and Algorithms,31508,128,0,128,Lloyd Hunter S,TR 1050-1205
CSCI 246,Discrete Structures,33580,84,0,84,Zhu Binhai,MWF 0800-0850
CSCI 305,Concepts/Programming Languages,30279,144,0,144,Lloyd Hunter S,MWF 0800-0850
CSCI 338,Computer Science Theory,30278,140,0,140,Zhu Binhai,MWF 1100-1150
CSCI 347,Data Mining,34852,75,0,75,Neeley Veronika,MWF 1310-1400
CSCI 366,Computer Systems,34889,60,0,60,Wittie Mike,MWF 0900-0950
CSCI 441,Computer Graphics,35118,65,0,65,Millman David,TR 1630-1745
CSCI 446,Artificial Intelligence,35119,63,0,63,Sheppard John W,TR 0925-1040
CSCI 455,Embedded Systems: Robotics,35117,84,0,84,Lloyd Hunter S,MWF 1200-1250
CSCI 468,Compilers,31511,80,0,80,Kahanda Indika M,MWF 1510-1600