-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcalendar.c
More file actions
78 lines (75 loc) · 1.79 KB
/
calendar.c
File metadata and controls
78 lines (75 loc) · 1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct
{
char *name;
int date;
char *description;
} Day;
// Function to create a calendar
Day *create()
{
Day *calendar = (Day *)malloc(sizeof(Day) * 7);
if (calendar = = NULL)
{
printf("Error allocating memory for calendar\n");
exit(1);
}
return calendar;
}
// Function to read data from the keyboard
void read(Day *calendar)
{
for (int i = 0; i < 7; i++)
{
printf("Enter the name of day %d: ", i + 1);
char *name = (char *)malloc(sizeof(char) * 100);
if (name == NULL)
{
printf("Error allocating memory for day name\n");
exit(1);
}
scanf("%s", name);
calendar[i].name = name;
printf("Enter the date of day %d: ", i + 1);
int date;
scanf("%d", &date);
calendar[i].date = date;
printf("Enter the description of activity for day %d: ", i + 1);
char *description = (char *)malloc(sizeof(char) * 100);
if (description == NULL) {
printf("Error allocating memory for activity description\n");
exit(1);
}
scanf("%s", description);
calendar[i].description = description;
}
}
// Function to print weeks activity details report on screen
void display(Day *calendar)
{
printf("\nWeek Activity Details Report\n");
printf("----------------------------\n");
for (int i = 0; i < 7; i++)
{
printf("%s %d: %s\n", calendar[i].name, calendar[i].date, calendar[i].description);
}
}
int main( )
{
// Create a calendar
Day *calendar = create();
// Read data from the keyboard
read(calendar);
// Display weeks activity details report on screen
display(calendar);
// Free the memory allocated for the calendar
for (int i = 0; i < 7; i++)
{
free(calendar[i].name);
free(calendar[i].description);
}
free(calendar);
return 0;
}