add remove command

This commit is contained in:
Zvonimir Rudinski
2023-12-16 17:22:03 +01:00
parent dd7c5d0a1c
commit f152a85d8a

15
main.c
View File

@@ -6,6 +6,7 @@ enum Command {
ADD = 'a', ADD = 'a',
MARK = 'm', MARK = 'm',
PRINT = 'p', PRINT = 'p',
REMOVE = 'r',
QUIT = 'q' QUIT = 'q'
}; };
@@ -30,6 +31,13 @@ void print_all_todos() {
} }
} }
void remove_todo_at_index(int index) {
for (int i = index; i < todos_count - 1; i++) {
todos[i] = todos[i + 1];
}
todos_count--;
}
int main(int argc, char **argv) { int main(int argc, char **argv) {
char cmd; char cmd;
int op_index = -1; int op_index = -1;
@@ -57,6 +65,13 @@ int main(int argc, char **argv) {
case PRINT: case PRINT:
print_all_todos(); print_all_todos();
break; break;
case REMOVE:
do {
printf("Enter todo index: ");
op_index = getc(stdin) - '0';
getc(stdin); // remove the newline character from the buffer
} while (op_index < 0 || op_index >= todos_count);
remove_todo_at_index(op_index);
case QUIT: case QUIT:
break; break;
default: default: