Introduction
A to-do list application helps users organize and track tasks that need to be completed.
In this program, we will implement a simple to-do list in C, where users can add, display, and delete tasks.
The application will allow a user to interact with a menu to perform the desired actions.
Objective
The goal of this project is to create a simple to-do list application in the C programming language.
Users will be able to:
- Add tasks to the to-do list.
- View the current list of tasks.
- Delete tasks when they are completed.
- Exit the application.
Code: Simple To-Do List Application in C
#include #include #define MAX_TASKS 100 #define MAX_LENGTH 200 // Structure for task struct Task { char description[MAX_LENGTH]; }; // Function prototypes void addTask(struct Task tasks[], int* taskCount); void deleteTask(struct Task tasks[], int* taskCount); void displayTasks(struct Task tasks[], int taskCount); void clearBuffer(); int main() { struct Task tasks[MAX_TASKS]; int taskCount = 0; int choice; while (1) { // Display menu printf("\n===== To-Do List Application =====\n"); printf("1. Add Task\n"); printf("2. Delete Task\n"); printf("3. Display Tasks\n"); printf("4. Exit\n"); printf("Enter your choice: "); scanf("%d", &choice); switch (choice) { case 1: addTask(tasks, &taskCount); break; case 2: deleteTask(tasks, &taskCount); break; case 3: displayTasks(tasks, taskCount); break; case 4: printf("Exiting the application. Goodbye!\n"); return 0; default: printf("Invalid choice. Please try again.\n"); } } return 0; } // Function to add a task to the list void addTask(struct Task tasks[], int* taskCount) { if (*taskCount < MAX_TASKS) { clearBuffer(); printf("Enter task description: "); fgets(tasks[*taskCount].description, MAX_LENGTH, stdin); tasks[*taskCount].description[strcspn(tasks[*taskCount].description, "\n")] = 0; // Remove trailing newline (*taskCount)++; printf("Task added successfully.\n"); } else { printf("Task list is full. Cannot add more tasks.\n"); } } // Function to delete a task from the list void deleteTask(struct Task tasks[], int* taskCount) { int taskId; if (*taskCount == 0) { printf("No tasks to delete.\n"); return; } printf("Enter task ID to delete (1 to %d): ", *taskCount); scanf("%d", &taskId); if (taskId < 1 || taskId > *taskCount) { printf("Invalid task ID.\n"); return; } // Shift tasks to delete the chosen task for (int i = taskId - 1; i < *taskCount - 1; i++) { tasks[i] = tasks[i + 1]; } (*taskCount)--; printf("Task deleted successfully.\n"); } // Function to display all tasks void displayTasks(struct Task tasks[], int taskCount) { if (taskCount == 0) { printf("No tasks available.\n"); return; } printf("\n===== Task List =====\n"); for (int i = 0; i < taskCount; i++) { printf("Task %d: %s\n", i + 1, tasks[i].description); } } // Function to clear the input buffer void clearBuffer() { while (getchar() != '\n'); }
Explanation of the Program Structure
The program is divided into several key functions:
- addTask: Allows the user to add a new task to the to-do list.
- deleteTask: Allows the user to delete a task from the list by specifying the task number.
- displayTasks: Displays all the tasks in the to-do list.
- clearBuffer: Ensures that any unwanted newline characters left in the input buffer are cleared before taking further input.
The main function implements a menu-driven interface where the user can choose what action to perform. The program continues to run in a loop until the user chooses to exit.
How to Run the Program
- Save the code in a C file, for example
todo_list.c
. - Compile the code using a C compiler, e.g.,
gcc todo_list.c -o todo_list
. - Run the compiled program by executing
./todo_list
on Linux/Mac ortodo_list.exe
on Windows. - Follow the on-screen menu to add, delete, or view tasks.