C: Calendar + linked lists

Started by
1 comment, last by TGS15 15 years, 1 month ago
Hi guys, I have been trying to write a calendar program that will be able to take diary entries from the user and whilst most of it works, i have run into some glitches and i was wondering if you had any ideas on how to fix them. Problem 1: For some reason when i input diary entries into the node (linear nodes), somehow the event_name isn't sent to the nodes. I have tried debbugging all the data is beng sent except the event name which is in chars whilst the rest are integers. Hence, in case 3 when I call the function to display nodes, it fails to display the contents the event name. Problem 2:I created a function to read data in the nodes and from the text file and compute the date as as a day of the week (i.e. if day is 2nd in a week, printf monday) but it's not working despite being correctly designed and called correctly. On top of that, I was wondering if there was a way of converting the month numbers read from the user entries and from the text file to words. for example when it reads a date from the text file, it should be able to say i.e. thursday 12 jan 2006. I have looked all over the web and I couldn't find any function that was relevant to the problem. if possible, i would also like to display the user's entries in the same format, but then again my compute_day_month function seems unable to do so. problem 3: In the case 5, i want to write the user's diary entries and the text file diary entries to one file but I recon that i would need to compute the total number of elements before writing them to an output file. Unfortunately, i can't find a way to count the number of elements in an struct before printing them out. I tried debbuging and couldn't find any other explanation for the crash that occurs in case 5.
/* project: Interactive user calendar application */

#include<stdio.h>
#include<stdlib.h>
#define num 30

typedef struct node {
  int event_year, event_month, event_day, start_time, finish_time;
  char event_name[100]; 
  struct node *next;
}node;
struct text {
	char diary_events[60];
}diary[num];//number of entries in diary file
int compute_day_month(int date, int month, int year)
{
	int a,m,y,day;
	date_validation(year,month,date);/*check if dates entered by user are valid*/
	a=((14-month)/12);
	y=(year-a);
	m=(month+(12*a)-2);
	day=(date+y+(y/4)-(y/100)+(y/400)+((31*m)/12))%7;
	if(day==0)
	printf("Sunday");
	if(day==1)
	printf("Monday");
	if(day==2)
	printf("Tuesday");
	if(day==3)
	printf("Wednesday");
	if(day==4)
	printf("Thursday");
	if(day==5)
	printf("Friday");
	if(day==6)
	printf("Saturday");
	return 0;
}


void display_events(struct node *head)
{
	struct node *n;
	for(n=head; n!=NULL; n=n->next)
	{
		printf("%d_%d_%d_%d_%d\n",n->event_year,n->event_month,n->event_day,n->start_time,n->finish_time);
		printf("%s\n",n->event_name);
	}
			
}

node* sequential_search(node *events, int date, int month, int year)
{
	for (;events!=NULL; events=events->next)
	{
		if (events->event_day==date || events->event_month==month || events->event_year==year)
			compute_day_month(date,month,year);
			printf("Diary Entries found:\n");
			printf("%d_%d_%d_%s",events->event_day, events->event_month,events->event_year,events->event_name);
	}
	return NULL;
}

struct node* mknode(int eyear, int month, int day, int begin_time, int end_time,char event_desc[50])
{
	struct node *np;

	np=(struct node*)malloc(sizeof(struct node));
	if(np)
	{
		np->event_year=eyear;
		np->event_month=month;
		np->event_day=day;
		np->start_time=begin_time;
		np->finish_time=end_time;
		np->event_name[100]=event_desc[50];
		np->next=NULL;
	}
	return np;
}

struct node* append_node(struct node** head, struct node* np)/*referred from textbook page- 6-6 */
{
	struct node* nod;

	if(*head==NULL)
		*head=np;
	else
	{
		for(nod=*head;nod->next!=NULL;nod=nod->next);
		/*above for loop has now reach the tail*/
		nod->next=np;
	}
	return np;
}

int date_validation(int eyear,int month,int day)
{
	int maximum_date=0;	
	if (month<1 || month>12 || day<0 || eyear<1753)
					{
						printf("You have entered an invalid date\n\n\n");
						return 0;
					}
	else {
	if (month==1||month==3||month==5||month==7||month==8||month==10||month==12)
			{
				maximum_date=31;
			}
		if (month==4 ||month==6 ||month==9 ||month==11)
			{
				maximum_date=30;
			}
		if (month==2)
		{
			if((eyear % 4)!=0)
				{
					maximum_date=28;
				}
			else if((eyear % 400)==0)
				{
					maximum_date=29;
				}
			else if((eyear % 100)==0)
				{
					maximum_date=28;
				}
			else
				{
					maximum_date=29;
				}
		}
		if(day>maximum_date)
			{
				printf("You have entered an invalid date for the event\n");
				return 0;
			}
		else
			{
				return 1;
			}
	}
}
int time_conversion(int y1, int y2)
{
	int hours1,hours0,minutes1,minutes0;
	struct node Node;
	Node.start_time=y1;
	Node.finish_time=y2;
	hours0=(Node.start_time/100);
	minutes0=(Node.start_time %100);
	hours1=(Node.finish_time/100);
	minutes1=(Node.finish_time % 100);
	printf("Start Time: %d hours %d minutes\n", hours0, minutes0);
	printf("Finish Time: %d hours %d minutes\n", hours1, minutes1);
	return 0;
}



int main(void)
{
int year[]={31,28,31,30,31,30,31,31,30,31,30,31};
int temp_word[200]={0};
int menu,eyear,month,day,begin_time,end_time,fail=0,count_events,no_events;
char event_desc[50],in_filename[30];
struct text Stored_text;
struct node *nod;
struct node *head=NULL;
FILE* input_file;
FILE* output_events=0;

printf("Welcome to the INTERACTIVE USER CALENDAR APPLICATION\n");
printf("====================================================\n\n");			
	do{	  /*The do while loop enables the user to continuously use the menu until exiting when they want to do so.*/
		printf("\nMENU\n");
		printf("====================================================\n\n");
		printf("1= Make new Diary Entry\n");
		printf("2= Read Diary Entries from text file \n");
		printf("3= Display all Diary Entries\n" );
		printf("4= Search for Diary Entry\n");
		printf("5= Write all Diary Entries to new file\n");
		printf("6= Book events to Chart\n");
		printf("7= End this session\n\n");
		printf("Choose any of the above choices\n");/*Asks the user to choose one of the menu options*/
		scanf("%d",&menu);

		switch(menu)
		{
			case 1:
						printf("Your choice: 1\n\n");
						printf("Make new Diary Entry:\n");
						printf("==========================================\n");
						printf("Enter the number of events you want to enter into the diary\n");
						scanf("%d",&no_events);

						for(count_events=0; count_events<no_events; count_events++)
						{
							printf("Event number : %d\n", count_events);
							printf("Enter day in this format: dd\n");
							scanf("%d",&day);
							printf("Enter month in this format: mm\n");
							scanf("%d",&month);
							printf("Enter year in this format: yyyy\n");
							scanf("%d",&eyear);
							fail=date_validation(eyear,month,day);	
							while (fail!=0)
							{
								printf("Enter start time in four digits:\n");
								scanf("%d",&begin_time);
								printf("Enter finish time in four digits:\n");
								scanf("%d",&end_time);
								time_conversion(begin_time, end_time);
								printf("Describe your event in text(use \"_\" to enter space between text):\n");
								scanf("%s",&event_desc);
								nod=mknode(eyear,month,day,begin_time,end_time,event_desc);
								append_node(&head,nod);
								fail=0;
							}
						}
						break;

			case 2:
						printf("Your choice: 2\n\n");
						printf("Read Diary Entries from text file: \n");
						printf("==========================================\n");
						printf("Enter location of file\n");
						scanf("%s",in_filename);
						// If no text file is found, print error message
						if((input_file=fopen(in_filename,"r"))==NULL)
							printf("Error,no text file found");
						else
						{//While file is open read events and print them out to user
						printf("Here are the registered events on the file: \n\n");
						while (fscanf(input_file,"%s",temp_word) != EOF){
								strcpy(Stored_text.diary_events,temp_word);
								printf("%s\n\n",Stored_text.diary_events);						
							}	
						}
						fclose(input_file);
						break;				

			case 3:
				
						printf("Your choice: 3\n\n");
						printf("Display all Diary Entries:\n");
						printf("==========================================\n");
						printf("Here are all the Diary Entries:\n");
						display_events(head);
						break ;						

			case 4:
				
						printf("Your choice: 4\n\n");
						printf("Search for a Diary Entry:\n");
						printf("==========================================\n");
						printf("Enter Date in the format: dd \n");
						scanf("%d",&day);
						printf("Enter Month in the format: mm\n");
						scanf("%d",&month);
						printf("Enter year in this format: yyyy\n");
						scanf("%d\n",&eyear);
						sequential_search(head,&day,&month,&eyear);
						break ; 

		    case 5:
				
						printf("Your choice: 5\n");
						printf("Write all Diary Entries to new file: \n");
						printf("==========================================\n");
						strcpy(Stored_text.diary_events,head);
						//copy elements in the lists to structure containg text diary entries
						printf("Enter location of the output filename\n");
						scanf("%s",output_events);
						if((output_events=fopen(Stored_text.diary_events,"r"))==NULL)
							printf("Error,no text file found");
						else{
						output_events=fopen(output_events, "wb");
						fwrite(diary,sizeof(struct text),30,output_events);
						}
						fclose(output_events);
						break ;

			case 6:
				
						printf("Your choice: 6\n");
						break ;

			case 7:
						break ;/*The Program Ends!*/				
		}
	}while(menu!=7);
	return 0;
}


He is the text of events that is to be read:
Quote:2002_03_21_1400_1530_world_cup_final 1996_06_18_1830_2145_euro_cup_final 2006_05_05_1600_1830_my_sweet_surprise_sixteenth_birthday 2008_11_05_0330_0400_history_is_made 1990_05_03_0800_0815_a_legend_is_born 1993_06_17_1500_1515_the_legends_sis_is_born 1993_05_23_1800_2115_Marseille_make_french_history 1998_07_17_1900_2115_Zizou_the_legend! 1949_09_24_1300_1700_Chairman_mao_peoples_republic 2009_03_12_1600_2100_amit_rodney_stuck_in_tiresome_lab 1904_10_30_1000_1700_lunch_with_royals 1980_03_12_1600_2100_bob_comes_to_power 1950_09_24_1300_1700_korean_war_starts 2009_03_13_1000_2100_amit_rodney_still_stuck_in_tiresome_lab 1993_06_17_1500_1515_the_legends_dad_is_away 1993_05_23_1800_2115_basile_boli_celebration_party
Thanks in advance for all your help, Kind regards, TGS
Advertisement
Quick glance (haven't looked really hard at it) for your problem #2 in your compute day and month your always returning 0, I do not believe that was what you intended to do here and if you intend to change the variables date, month, and year you need to pass them by reference in order for the change to take effect beyond the scope of that particular function. Of course if you intend for those numbers to be the event variables then you could (assuming your insertion method is correct) just call node->event_x and set that equal to your newly formatted dates. Hope that gets you started.
I have almost completed the program although I can't FWITE or FREAD my linked lists to and from a file. This is in case 5 and everytime I run it, it returns nothing and closes the program. Does anyone know how to do this??
#include<stdio.h>#include<stdlib.h>#define NUM 1000typedef struct node {  int event_year, event_month, event_day, start_time, finish_time;  char event_name[100];   struct node *next;}node;int compute_day_month(int date, int month, int year){	int a,m,y,day;	date_validation(year,month,date);/*check if dates entered by user are valid*/	a=((14-month)/12);	y=(year-a);	m=(month+(12*a)-2);	if(month==1)		printf("\nJan\n");	if(month==2)		printf("\nFeb\n");	if(month==3)		printf("\nMar\n");	if(month==4)		printf("\nApr\n");	if(month==5)		printf("\nMay\n");	if(month==6)		printf("\nJun\n");	if(month==7)		printf("\nJul\n");	if(month==8)		printf("\nAug\n");	if(month==9)		printf("\nSep\n");	if(month==10)		printf("\nOct\n");	if(month==11)		printf("\nNov\n");	if(month==12)		printf("\nDec\n");	day=(date+y+(y/4)-(y/100)+(y/400)+((31*m)/12))%7;	if(day==0)	printf("Sunday\n");	if(day==1)	printf("Monday\n");	if(day==2)	printf("Tuesday\n");	if(day==3)	printf("Wednesday\n");	if(day==4)	printf("Thursday\n");	if(day==5)	printf("Friday\n");	if(day==6)	printf("Saturday\n");	return 1;}void display_events(struct node *head){	struct node *nod;	for(nod=head; nod!=NULL; nod=nod->next){		printf("%d_%d_%d_%d_%d_%s\n",nod->event_day,nod->event_month,nod->event_year,nod->start_time,nod->finish_time,nod->event_name);		printf("\n");	}			}node* sequential_search(node *head, int date, int month, int year){	for (;head!=NULL; head=head->next)	{		if ((head->event_day==date) & (head->event_month==month) & (head->event_year==year))		{			compute_day_month(date,month,year);			printf("\nStart Time:%d \nFinish Time:%d \n%s\n",head->start_time,head->finish_time,head->event_name);		}		else 			printf("No events found, redefine your search criteria");	}	return NULL;}int find_structure_events(int scan_year,int scan_month,int scan_day,int chart_start_time,struct node *head){	struct node* nod;	int found_count=0;	for(nod=head; nod!=NULL; nod=nod->next)	{		if(scan_year==nod->event_year && scan_month==nod->event_month && scan_day==nod->event_day && (chart_start_time>=nod->start_time && chart_start_time<=nod->finish_time))			found_count=found_count+1;				else			found_count=0;	}	return found_count;}struct node* mknode(int eyear, int month, int day, int begin_time, int end_time,char event_desc[50]){	struct node *np;	np=(struct node*)malloc(sizeof(struct node));	if(np)	{		np->event_year=eyear;		np->event_month=month;		np->event_day=day;		np->start_time=begin_time;		np->finish_time=end_time;		strcpy(np->event_name,event_desc);		np->next=NULL;	}	return np;}struct node* append_node(struct node** head, struct node* np)/*referred from textbook page- 6-6 */{	struct node* nod;	if(*head==NULL)		*head=np;	else	{		for(nod=*head;nod->next!=NULL;nod=nod->next);		/*above for loop has now reach the tail*/		nod->next=np;	}	return np;}struct text {	char diary_events[];};int date_validation(int eyear,int month,int day){	int maximum_date=0;		if (month<1 || month>12 || day<0 || eyear<1753)					{						printf("You have entered an invalid date\n\n\n");						return 0;					}	else {	if (month==1||month==3||month==5||month==7||month==8||month==10||month==12)			{				maximum_date=31;			}		if (month==4 ||month==6 ||month==9 ||month==11)			{				maximum_date=30;			}		if (month==2)		{			if((eyear % 4)!=0)				{					maximum_date=28;				}			else if((eyear % 400)==0)				{					maximum_date=29;				}			else if((eyear % 100)==0)				{					maximum_date=28;				}			else				{					maximum_date=29;				}		}		if(day>maximum_date)			{				printf("You have entered an invalid date for the event\n");				return 0;			}		else			{				return 1;			}	}}int time_conversion(int y1, int y2){	int hours1,hours0,minutes1,minutes0;	struct node Node;	Node.start_time=y1;	Node.finish_time=y2;	hours0=(Node.start_time/100);	minutes0=(Node.start_time %100);	//if user enters minutes bigger than 60, the time is adjusted appropriately	while (minutes0>59 && minutes0<100)	{		hours0=hours0+1;		minutes0=minutes0-60;	}	hours1=(Node.finish_time/100);	minutes1=(Node.finish_time % 100);	//if user enters minutes bigger than 60, the time is adjusted appropriately	while (minutes1>59 && minutes1<100)	{		hours1=hours1+1;		minutes1=minutes1-60;	}	printf("Start Time: %d hours %d minutes\n", hours0, minutes0);	printf("Finish Time: %d hours %d minutes\n", hours1, minutes1);	}int main(void){int temp_word[200]={0}, outbuff[NUM];int menu,eyear,month,day,begin_time,end_time,fail=0,count_events,no_events,rvalue,nread,n;char event_desc[50],in_filename[50]={0},file_event_name[50],output_file_name[30];//int buffer[200];int f_year,f_month,f_day,f_start_time,f_finish_time;struct node *nod;struct node *head=NULL;struct node *backhead;FILE* input_file;FILE* output_events;int t,found_count,scan_year,scan_month,scan_day,chart_time,chart_start_time;int time_array[33]={900,915,930,945,1000,1015,1030,1045,1100,1115,1130,1145,1200,1215,1230,1245,1300,1315,1330,1345,1400,1415,1430,1445,1500,1515,1530,1545,1600,1615,1630,1645,1700};printf("Welcome to the INTERACTIVE USER CALENDAR APPLICATION\n");printf("====================================================\n\n");				do{	  /*The do while loop enables the user to continuously use the menu until exiting when they want to do so.*/		printf("\nMENU\n");		printf("====================================================\n\n");		printf("1= Make new Diary Entry\n");		printf("2= Read Diary Entries from text file \n");		printf("3= Display all Diary Entries\n" );		printf("4= Search for Diary Entry\n");		printf("5= Write all Diary Entries to new file\n");		printf("6= Book events to Chart\n");		printf("7= End this session\n\n");		printf("Choose any of the above choices\n");/*Asks the user to choose one of the menu options*/		scanf("%d",&menu);		switch(menu)		{			case 1:						printf("Your choice: 1\n\n");						printf("Make new Diary Entry:\n");						printf("==========================================\n");						printf("Enter the number of events you want to enter into the diary\n");						scanf("%d",&no_events);						for(count_events=0; count_events<no_events; count_events++)						{							printf("Event number : %d\n", count_events);							printf("Enter day in this format: dd\n");							scanf("%d",&day);							printf("Enter month in this format: mm\n");							scanf("%d",&month);							printf("Enter year in this format: yyyy\n");							scanf("%d",&eyear);							fail=date_validation(eyear,month,day);								while (fail!=0)							{								printf("Enter start time in four digits and in intervals of 00, 15, 30, 45:\n");								scanf("%d",&begin_time);								printf("Enter finish time in four digits and in intervals of 00, 15, 30, 45:\n");								scanf("%d",&end_time);								time_conversion(begin_time, end_time);								//make sure that user enters a finish time larger and not equal to start time								if (begin_time>=end_time)								{printf("Error:Invalid times entered\n");								return 0;}								printf("Describe event in 50 characters(use \"_\" for spaces):\n");								scanf("%s",&event_desc);								nod=mknode(eyear,month,day,begin_time,end_time,event_desc);								append_node(&head,nod);								fail=0;							}						}						break;			case 2:						printf("Your choice: 2\n\n");						printf("Read Diary Entries from text file: \n");						printf("==========================================\n");						nod=(node*)(malloc(1000*sizeof(struct node)));						printf("Enter location of file\n");						scanf("%s",in_filename);						input_file=fopen(in_filename,"r");						// If no text file is found, print error message						if(input_file==NULL)						{							printf("Error,no text file found");							return 0;						}						rvalue=0;						rvalue=fscanf(input_file, "%d", &f_year);						while (rvalue!=EOF)						{							rvalue=fscanf(input_file, "%d", &f_month);							rvalue=fscanf(input_file, "%d", &f_day);							rvalue=fscanf(input_file, "%d", &f_start_time);							rvalue=fscanf(input_file, "%d", &f_finish_time);							rvalue=fscanf(input_file, "%s", &file_event_name);							nod=mknode(f_year,f_month,f_day,f_start_time,f_finish_time,file_event_name);							append_node(&head,nod);							rvalue=fscanf(input_file,"%d", &f_year);						}fclose(input_file);						display_events(head);/*displays diary entries on the file and those the user has entered*/						break;								case 3:										printf("Your choice: 3\n\n");						printf("Display all Diary Entries:\n");						printf("==========================================\n");						printf("Here are all the Diary Entries:\n");						display_events(head);						break ;									case 4:										printf("Your choice: 4\n\n");						printf("Search for a Diary Entry:\n");						printf("==========================================\n");						printf("Enter Date in the format: dd \n");						scanf("%d",&day);						printf("Enter Month in the format: mm\n");						scanf("%d",&month);						printf("Enter year in this format: yyyy\n");						scanf("%d",&eyear);						sequential_search(head,day,month,eyear);						break ; 		    case 5:										printf("Your choice: 5\n");						printf("Write all Diary Entries to new file: \n");						printf("==========================================\n");						printf("Enter Location of output file\n");						scanf("%s",output_file_name);												for(n=0;n<1000; n++)							outbuff[n]=n;						output_events=fopen(output_file_name,"wb");						fwrite(head,sizeof(struct node),1000,output_events);  /* Write to file*/						fclose(output_events);						input_file=fopen(output_file_name,"rb");/*open file*/						nread=fread(backhead,sizeof(struct node),1000,input_file); /*read numb of items wrote to file*/						printf(" %d items read from the file\n", nread);						return 0;						break ;				case 6:																printf("Your choice: 6\n");						printf("enter the year\n");						scanf("%d",&scan_year);						printf("enter the month\n");						scanf("%d",&scan_month);						printf("enter the day\n");						scanf("%d",&scan_day);												for(t=0;t<33;t++)						{							chart_start_time=time_array[t];							found_count=find_structure_events(scan_year,scan_month,scan_day,chart_start_time,head);														chart_time=(chart_start_time%100);							if (chart_time==0)								printf("|");																				if (found_count==1)								printf("#");										else if (found_count>1)								printf("x");							else printf(".");						}						break ;			case 7:						break ;/*The Program Ends!*/						}	}while(menu!=7);	return 0;}


Cheers in advance or your help.

This topic is closed to new replies.

Advertisement