diff --git a/build.sh b/build.sh index 1bb27a0..a34c540 100644 --- a/build.sh +++ b/build.sh @@ -1 +1 @@ -cc main.c -o todd +cc todo.c main.c -o todd diff --git a/main.c b/main.c index 5465cf2..927c06e 100644 --- a/main.c +++ b/main.c @@ -1,14 +1,6 @@ #include -#include #include - -#define MAX_TODO_TITLE_LENGTH 32 -#define MAX_TODOS 10 - -typedef struct { - char title[MAX_TODO_TITLE_LENGTH]; - bool completed; -} TodoItem; +#include "todo.h" enum Command { ADD = 'a', @@ -17,26 +9,6 @@ enum Command { QUIT = 'q' }; -TodoItem create_todo(const char *title) { - TodoItem item; - strncpy(item.title, title, MAX_TODO_TITLE_LENGTH); - item.completed = false; - return item; -} - -void mark_todo(TodoItem *item, bool completed) { - (*item).completed = completed; -} - -void print_todo(TodoItem item) { - printf("%s - ", item.title); - if (item.completed) { - printf("Completed\n"); - } else { - printf("Not Completed\n"); - } -} - TodoItem todos[MAX_TODOS]; int todos_count = 0; @@ -44,6 +16,8 @@ void add_todo() { char title[MAX_TODO_TITLE_LENGTH]; printf("Enter todo title: "); fgets(title, MAX_TODO_TITLE_LENGTH - 1, stdin); + // remove the newline character from the buffer + title[strlen(title) - 1] = '\0'; TodoItem item = create_todo(title); todos[todos_count] = item; @@ -52,7 +26,7 @@ void add_todo() { void print_all_todos() { for (int i = 0; i < todos_count; i++) { - print_todo(todos[i]); + print_todo(&todos[i]); } } diff --git a/todo.c b/todo.c new file mode 100644 index 0000000..7c05c24 --- /dev/null +++ b/todo.c @@ -0,0 +1,23 @@ +#include "todo.h" +#include +#include + +TodoItem create_todo(const char *title) { + TodoItem item; + strncpy(item.title, title, MAX_TODO_TITLE_LENGTH); + item.completed = false; + return item; +} + +void mark_todo(TodoItem *item, bool completed) { + (*item).completed = completed; +} + +void print_todo(TodoItem *item) { + printf("%s - ", item->title); + if (item->completed) { + printf("Completed\n"); + } else { + printf("Not Completed\n"); + } +} diff --git a/todo.h b/todo.h new file mode 100644 index 0000000..057d48b --- /dev/null +++ b/todo.h @@ -0,0 +1,15 @@ +#ifndef TODO_H +#define TODO_H +#include +#define MAX_TODO_TITLE_LENGTH 32 +#define MAX_TODOS 10 + +typedef struct { + char title[MAX_TODO_TITLE_LENGTH]; + bool completed; +} TodoItem; + +TodoItem create_todo(const char *title); +void mark_todo(TodoItem *item, bool completed); +void print_todo(TodoItem *item); +#endif