#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Structure to represent a todo item
typedef struct {
char title[100];
char description[500];
} TodoItem;
// Function to display the menu options
void displayMenu() {
printf("\n===== Todo App =====\n");
printf("1. Add a new todo\n");
printf("2. View all todos\n");
printf("3. Exit\n");
printf("====================\n\n");
printf("Enter your choice: ");
}
// Function to add a new todo
void addTodoItem(TodoItem* todos, int* count) {
getchar(); // Clear the newline character from the input buffer
printf("Enter title: ");
fgets(todos[*count].title, sizeof(todos[*count].title), stdin);
todos[*count].title[strcspn(todos[*count].title, "\n")] = '\0'; // Remove trailing newline character
printf("Enter description: ");
fgets(todos[*count].description, sizeof(todos[*count].description), stdin);
todos[*count].description[strcspn(todos[*count].description, "\n")] = '\0'; // Remove trailing newline character
(*count)++;
printf("Todo added successfully.\n");
}
// Function to view all todos
void viewTodos(TodoItem* todos, int count) {
if (count == 0) {
printf("No todos found.\n");
return;
}
printf("===== Todos =====\n");
for (int i = 0; i < count; i++) {
printf("Todo %d:\n", i + 1);
printf("Title: %s\n", todos[i].title);
printf("Description: %s\n\n", todos[i].description);
}
printf("=================\n");
}
int main() {
TodoItem todos[100];
int count = 0;
int choice;
while (1) {
displayMenu();
scanf("%d", &choice);
switch (choice) {
case 1:
addTodoItem(todos, &count);
break;
case 2:
viewTodos(todos, count);
break;
case 3:
printf("Goodbye!\n");
exit(0);
default:
printf("Invalid choice.\n");
}
}
return 0;
}
This todo app allows you to add new todos with a title and description, view all todos, and exit the app. The todo items are stored in an array of TodoItem structures. Keep in mind that this is a basic implementation and does not include features like editing or deleting todos.