Build a stylish To-Do List application in C programming with our detailed guide. Learn how to create a visually appealing console application.
Introduction
Creating a To-Do List application is a great way to practice C programming. This project is simple yet covers key concepts like arrays, strings, and functions. In this blog, we'll guide you step-by-step on how to build a To-Do List application in C programming. By the end, you'll have a fully functional program that helps users manage their tasks efficiently.
Step-by-Step Guide to Create a To-Do List Application in C
Step 1: Setting Up the Environment
Before diving into the code, ensure you have a C compiler installed. You can use any IDE like Code::Blocks, Dev-C++, or even a simple text editor with GCC.
Step 2: Planning the To-Do List Application
Let's break down the features of our To-Do List application:
- Add tasks to the list.
- View the tasks.
- Mark tasks as completed.
- Delete tasks.
We'll use an array of strings to store the tasks, and an integer array to keep track of task completion.
Step 3: Writing the Code
Now, let's start coding. Below is the complete code for the To-Do List application:
#include <stdio.h> #include <string.h> #include <windows.h> // Include Windows API for color #define MAX_TASKS 100 #define MAX_LENGTH 100 // Color codes for Windows CMD #define RESET 15 #define BLUE 9 #define GREEN 10 #define RED 12 #define YELLOW 14 #define BOLD 8 void setColor(int color) { HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); SetConsoleTextAttribute(hConsole, color); } void addTask(char tasks[][MAX_LENGTH], int *taskCount); void viewTasks(char tasks[][MAX_LENGTH], int *completed, int taskCount); void completeTask(int *completed, int taskCount); void deleteTask(char tasks[][MAX_LENGTH], int *completed, int *taskCount); void printHeader(); int main() { char tasks[MAX_TASKS][MAX_LENGTH]; int completed[MAX_TASKS] = {0}; int taskCount = 0; int choice; do { printHeader(); setColor(BLUE); printf("1. Add Task\n"); printf("2. View Tasks\n"); printf("3. Mark Task as Completed\n"); printf("4. Delete Task\n"); printf("5. Exit\n"); setColor(YELLOW); printf("Choose an option: "); setColor(RESET); scanf("%d", &choice); getchar(); // Clear the input buffer switch (choice) { case 1: addTask(tasks, &taskCount); break; case 2: viewTasks(tasks, completed, taskCount); break; case 3: completeTask(completed, taskCount); break; case 4: deleteTask(tasks, completed, &taskCount); break; case 5: setColor(GREEN); printf("Exiting the program.\n"); setColor(RESET); break; default: setColor(RED); printf("Invalid choice! Please try again.\n"); setColor(RESET); } } while (choice != 5); return 0; } void printHeader() { setColor(BLUE); printf("\n***************************************\n"); printf("* Stylish To-Do List Menu *\n"); printf("***************************************\n"); setColor(RESET); } void addTask(char tasks[][MAX_LENGTH], int *taskCount) { if (*taskCount >= MAX_TASKS) { setColor(RED); printf("Task list is full!\n"); setColor(RESET); return; } setColor(YELLOW); printf("Enter the task: "); setColor(RESET); fgets(tasks[*taskCount], MAX_LENGTH, stdin); tasks[*taskCount][strcspn(tasks[*taskCount], "\n")] = '\0'; // Remove newline character (*taskCount)++; setColor(GREEN); printf("Task added successfully.\n"); setColor(RESET); } void viewTasks(char tasks[][MAX_LENGTH], int *completed, int taskCount) { if (taskCount == 0) { setColor(RED); printf("No tasks to display.\n"); setColor(RESET); return; } setColor(BLUE); printf("\nTo-Do List:\n"); setColor(RESET); for (int i = 0; i < taskCount; i++) { setColor(GREEN); printf("%d. %s [%s]\n", i + 1, tasks[i], completed[i] ? "Completed" : "Not Completed"); setColor(RESET); } } void completeTask(int *completed, int taskCount) { int taskNumber; setColor(YELLOW); printf("Enter the task number to mark as completed: "); setColor(RESET); scanf("%d", &taskNumber); if (taskNumber < 1 || taskNumber > taskCount) { setColor(RED); printf("Invalid task number!\n"); setColor(RESET); return; } completed[taskNumber - 1] = 1; setColor(GREEN); printf("Task marked as completed.\n"); setColor(RESET); } void deleteTask(char tasks[][MAX_LENGTH], int *completed, int *taskCount) { int taskNumber; setColor(YELLOW); printf("Enter the task number to delete: "); setColor(RESET); scanf("%d", &taskNumber); if (taskNumber < 1 || taskNumber > *taskCount) { setColor(RED); printf("Invalid task number!\n"); setColor(RESET); return; } for (int i = taskNumber - 1; i < *taskCount - 1; i++) { strcpy(tasks[i], tasks[i + 1]); completed[i] = completed[i + 1]; } (*taskCount)--; setColor(GREEN); printf("Task deleted successfully.\n"); setColor(RESET); }
Step 4: Explanation of the Code
- Header Files and Macros:
We includestdio.h
andstring.h
for input/output and string manipulation. We defineMAX_TASKS
as 100 andMAX_LENGTH
as 100, which sets the maximum number of tasks and the maximum length of each task, respectively. - Functions:
addTask
: Adds a new task to the list.viewTasks
: Displays all tasks and their completion status.completeTask
: Marks a task as completed.deleteTask
: Removes a task from the list.
- Main Function:
The main function presents a menu to the user, allowing them to add, view, complete, or delete tasks. The program runs in a loop until the user chooses to exit.
Step 5: Compiling and Running the Program
To compile the code, open your terminal or IDE and run the following command:
gcc -o todo_list todo_list.c
This will compile the code and create an executable named todo_list
. To run the program, use the command:
todo_list
The program will start, and you can interact with the To-Do List through the menu.
Conclusion
Creating a To-Do List application in C programming is a great project to enhance your coding skills. This project helps you understand arrays, strings, and functions in C. By following this guide, you now have a simple yet functional To-Do List application that you can customize further. Keep experimenting and adding more features to make it even more powerful!
This blog provides a complete guide to creating a To-Do List application in C programming. By following these steps, you'll not only improve your coding skills but also learn how to manage tasks effectively using a simple program.
That’s a wrap!
I hope you enjoyed this article
Did you like it? Let me know in the comments below 🔥 and you can support me by buying me a coffee.
And don’t forget to sign up to our email newsletter so you can get useful content like this sent right to your inbox!
Thanks!
Faraz 😊