Skip to content

Instantly share code, notes, and snippets.

@flightcrank
Created April 27, 2012 09:33
Show Gist options
  • Save flightcrank/2507802 to your computer and use it in GitHub Desktop.
Save flightcrank/2507802 to your computer and use it in GitHub Desktop.
challange_1_i
/*
create a program that will allow you to enter events organizable by hour.
There must be menu options of some form, and you must be able to easily edit, add, and delete events
without directly changing the source code.
(note that by menu i dont necessarily mean gui. as long as you can easily access the different
options and receive prompts and instructions telling you how to use the program, it will probably be fine)
*/
#include <stdio.h>
#include <string.h>
struct event {
char name[25];
char time[6];
};
struct event events[5];
int num_event = 0;
int end = 0;
void print_events() {
printf("\n --- Events --- \n");
int i;
for (i = 0; i < num_event; i++) {
if (num_event > 0 ) {
if (strcmp(events[i].name, "") == 0) {
printf("#%d event erased\n", i+1);
} else {
printf("#%d: event %s, time %s\n", i+1, events[i].name, events[i].time);
}
}
}
}
void print_menu() {
printf("\n --- Main Menu --- \n");
printf("1: add events\n");
printf("2: edit events\n");
printf("3: delete events\n");
printf("4: view events\n");
printf("0: exit program\n");
int selection = -1;
scanf("%d", &selection);
int sel = -1;
switch (selection) {
case 1://add events
if (num_event >= 5) {
printf("Max number of events reached\n");
break;
}
printf("Enter event name: \n");
scanf("%s",events[num_event].name);
printf("Enter event time: \n");
scanf("%s",events[num_event].time);
num_event++;
printf("Event \"%s\" has been added at time \"%s\" \n",events[0].name, events[0].time);
print_events();
break;
case 2://edit events
print_events();
printf("Select event number to edit\n");
sel = -1;
scanf("%d", &sel);
printf("event #%d selected\n", sel);
printf("enter event name:\n");
scanf("%s",events[sel - 1].name);
printf("enter event time:\n");
scanf("%s",events[sel - 1].time);
printf("Event \"%d\" has been edited to \"%s\" @ \"%s\" \n", sel, events[sel - 1].name, events[sel - 1].time);
print_events();
break;
case 3://delete event
print_events();
printf("Select event number to delete\n");
sel = -1;
scanf("%d", &sel);
strcpy(events[sel - 1].name, "");
strcpy(events[sel - 1].time, "");
printf("Event \"%d\" has been deleted \n", sel);
print_events();
break;
case 4://view events
print_events();
break;
default:
printf("exiting program\n");
end = 1;
break;
}
}
int main() {
while (1) {
print_menu();
if (end == 1) {
break;
}
}
return 0;
}
@flightcrank
Copy link
Author

well it meets the requitements. i dont know what they mean "organisable by hour". Sorted maybe ?

theres no input validation, or buffer overflow checking.
it would also benefit from a more dynamic array.
limited to 5 events.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment