I have been working on this project for a week straight and it's due at midnight tonight. I'm a freshman computer science student and new to programming so I cannot see what's wrong with this code. The code is supposed to read in event titles, event times, and event dates into a linked list and sort is alphabetically using the event titles.
This is the input from the file:
Birthday 12 30 10 01 2018 Wedding 06 30 06 15 2018 Seminar 05 00 02 15 2019 Birthday 04 00 06 15 2018 Anniversary 08 30 12 09 2019
For some reason, it never connects the wedding event to the linked list and also when printing the entire linked list there is a blank node as the head node. I have been working on this forever and even when tracing the code I cannot figure out what's going wrong.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//struct for event time
typedef struct{
int hour;
int minute;
} event_time_t;
//struct for event date
typedef struct{
int month;
int day;
int year;
} event_date_t;
//struct for all event info
struct event{
char event_title[20];
event_time_t event_time;
event_date_t event_date;
struct event *next;
};
typedef struct event event_t;
void add_events (event_t **head_ptr);
void print_event(event_t *head_ptr);
void print_slected_event (event_t *head_ptr, int month, int day, int year);
int main () {
event_t *head_ptr = malloc(sizeof(event_t));
add_events(&head_ptr);
print_event(head_ptr);
print_slected_event(head_ptr,6,15,2018);
return 0;
}
void add_events (event_t **head_ptr){
event_t *temp;
event_t *temp_head = *head_ptr;
event_t *new_node;
scanf(" %s",temp_head->event_title);
scanf("%d",&temp_head->event_time.hour);
scanf("%d",&temp_head->event_time.minute);
scanf("%d",&temp_head->event_date.month);
scanf("%d",&temp_head->event_date.day);
scanf("%d",&temp_head->event_date.year);
temp_head->next = NULL;
while(!feof(stdin)){
new_node = malloc(sizeof(event_t));
scanf(" %s",new_node->event_title);
scanf("%d",&new_node->event_time.hour);
scanf("%d",&new_node->event_time.minute);
scanf("%d",&new_node->event_date.month);
scanf("%d",&new_node->event_date.day);
scanf("%d",&new_node->event_date.year);
if(temp_head->next == NULL){
temp_head->next = new_node;
}
else if(strcmp(temp_head->event_title,new_node->event_title)>0){
new_node->next = temp_head;
temp_head = new_node;
*head_ptr = temp_head;
}
else{
temp = temp_head;
while(temp->next!=NULL){
if(strcmp(temp->event_title,new_node->event_title)==0){
break;
}
if(strcmp(temp->event_title,new_node->event_title)<0){
break;
}
temp = temp->next;
}
new_node->next = temp->next;
temp->next = new_node;
}
}
}
void print_event(event_t *head_ptr){
event_t *temp;
temp = malloc(sizeof(event_t));
temp = head_ptr;
printf("Scedule of Events:\n");
while(temp->next!=NULL){
printf("\t%-13s at: %02d:%02d ",temp->event_title,temp->event_time.hour,temp->event_time.minute);
printf("on: %02d/%02d/%d\n",temp->event_date.month,temp->event_date.day,temp->event_date.year);
temp = temp->next;
}
}
void print_slected_event (event_t *head_ptr, int month, int day, int year){
event_t *temp;
temp = malloc(sizeof(event_t));
temp = head_ptr;
printf("Date: %02d/%02d/%d\n",month,day,year);
printf("Events:\n");
while(temp->next!=NULL){
if(temp->event_date.month == month){
if(temp->event_date.day == day){
if(temp->event_date.year == year){
printf("\t%-13s at: %02d:%02d\n",temp->event_title,temp->event_time.hour,temp->event_time.minute);
}
}
}
temp = temp->next;
}
}