Table of Contents
Introduction : To-Do List
Hello Everyone, here we Build a To-Do List in C Language. This is the console based application designed to manage tasks. It allows user to add tasks, mark them as completed, delete task, and view the list of tasks. Let’s Build this amazing Project…
Overview of Code Structure
Here is the Overview of Code Structure of To-Do List,
- Header File: Here we Includes necessary standard libraries for input/output operations, memory allocations, and string manipulation.
- Task Structure: Here we defines a Structure ‘Task’ representing a single task with a description and completion status.
- Function Prototype: Here we declares function prototype for adding, displaying, marking as completed, and deleting tasks.
- Main Function: Here we implements a menu-driven loop to interact with the user, presenting options and executing actions based on user input.
Free E-Books
Output
Project in Brief
Here we will discuss about the every possible part of the Project
Part – 1 Header File
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
In Part – 1, we include necessary standard libraries for input/output operations, memory allocations, and string manipulation.
Free Hand-Written Note
Part – 2 Task Structure
// Define structure for Task
struct Task {
char description[100];
int completed;
};
In the Part – 2, we defines a Structure ‘Task’ representing a single task with a description and completion status.
Part – 3 Function Prototype
In Function Prototype we declare Functions for Add, Display, Mark as Completed, and Deletes Task. So here is brief about these 4 Functions.
1. Function for Adding Task
// Function to add a new task
void addTask(struct Task *tasks, int *numTasks) {
printf("Enter task description: ");
scanf(" %[^\n]", tasks[*numTasks].description);
tasks[*numTasks].completed = 0; // Set completed status to 0 (not completed)
(*numTasks)++;
printf("Task added successfully.\n");
}
This Function allows users to add a new task to the list. It prompts the user for description, reads it from the standard input, sets the completion status to 0, increment the task count, and notifies the user about the successful adition.
2. Function for Displaying Task
// Function to display all tasks
void displayTasks(struct Task *tasks, int numTasks) {
printf("\nTasks:\n");
for (int i = 0; i < numTasks; i++) {
printf("%d. [%s] %s\n", i + 1, tasks[i].completed ? "X" : " ", tasks[i].description);
}
}
This function displays all the tasks currently in the list. It prints each task’s index completion status (represented by “X” for completed tasks), and description to the console.
3. Marking Task as Completed
// Function to mark a task as completed
void markCompleted(struct Task *tasks, int numTasks, int taskIndex) {
if (taskIndex >= 1 && taskIndex <= numTasks) {
tasks[taskIndex - 1].completed = 1;
printf("Task marked as completed.\n");
} else {
printf("Invalid task index.\n");
}
}
This Function is allow user to mark a specific task as completed. It checks if the provided task index is valid, and if so, it updates the completion status of that task to indicate completion.
4. Function For Deleting Task
// Function to delete a task
void deleteTask(struct Task *tasks, int *numTasks, int taskIndex) {
if (taskIndex >= 1 && taskIndex <= *numTasks) {
// Shift elements to the left to overwrite the deleted task
for (int i = taskIndex - 1; i < *numTasks - 1; i++) {
strcpy(tasks[i].description, tasks[i + 1].description);
tasks[i].completed = tasks[i + 1].completed;
}
(*numTasks)--;
printf("Task deleted successfully.\n");
} else {
printf("Invalid task index.\n");
}
}
This Function is allow user to delete the specific task from the list. It checks if the provided task index is valid, and if so, it shifts the element in the array to overwrite the deleted task.
Part – 4 Main Function
For understand briefly we divide the Main Function is Three Parts:
1. Initialization and Menu Display
int main() {
struct Task tasks[100]; // Declare an array to store tasks
int numTasks = 0; // Initialize the number of tasks to 0
int choice; // Variable to store user choice
int taskIndex; // Variable to store task index for marking or deleting
do {
// Display menu options
printf("\nTo-Do List\n");
printf("1. Add Task\n");
printf("2. Display Tasks\n");
printf("3. Mark Task as Completed\n");
printf("4. Delete Task\n");
printf("5. Exit\n");
printf("Enter your choice: ");
// Read user choice
scanf("%d", &choice);
// Process user choice with switch-case
switch (choice) {
// Cases for each menu option
}
} while (choice != 5); // Repeat until user chooses to exit
return 0; // Return 0 to indicate successful execution
}
Here, the main function initializes necessary variables including an array to store tasks (‘tasks’), the number of tasks (‘numTasks’), the user’s choice (‘choice’), and the task index for marking and deleting (‘taskIndex’). And then enters the do-while Loop to repeatedly display the menu options and process the user’s choice until the user chooses to exit (Option 5).
2. Menu Processing
The Main Function’s switch case structure handles the user’s choice by calling corresponding functions for each menu option:
- Case 1: Calls ‘addTasks()’ function to add new task.
- Case 2: Calls ‘displayTasks()’ function to display all tasks.
- Case 3: Prompts the user for a task index and calls ‘markCompleted()’ function to mark the task as completed.
- Case 4: Prompts the user for a task index and calls ‘deleteTask()’ function to delete task.
- Case 5: Exit the Program.
3. Exit the Program
} while (choice != 5); // Repeat until user chooses to exit
return 0; // Return 0 to indicate successful execution
In This Part concludes the main function by ensuing the program continues to loop until the user chooses to exit (Option 5). It returns 0 to indicate successful execution of the program.
Complete Project Codes
Here is the Complete Project Codes
#include <stdio.h> #include <stdlib.h> #include <string.h> // Define structure for Task struct Task { char description[100]; int completed; }; // Function to add a new task void addTask(struct Task *tasks, int *numTasks) { printf("Enter task description: "); scanf(" %[^\n]", tasks[*numTasks].description); tasks[*numTasks].completed = 0; // Set completed status to 0 (not completed) (*numTasks)++; printf("Task added successfully.\n"); } // Function to display all tasks void displayTasks(struct Task *tasks, int numTasks) { printf("\nTasks:\n"); for (int i = 0; i < numTasks; i++) { printf("%d. [%s] %s\n", i + 1, tasks[i].completed ? "X" : " ", tasks[i].description); } } // Function to mark a task as completed void markCompleted(struct Task *tasks, int numTasks, int taskIndex) { if (taskIndex >= 1 && taskIndex <= numTasks) { tasks[taskIndex - 1].completed = 1; printf("Task marked as completed.\n"); } else { printf("Invalid task index.\n"); } } // Function to delete a task void deleteTask(struct Task *tasks, int *numTasks, int taskIndex) { if (taskIndex >= 1 && taskIndex <= *numTasks) { // Shift elements to the left to overwrite the deleted task for (int i = taskIndex - 1; i < *numTasks - 1; i++) { strcpy(tasks[i].description, tasks[i + 1].description); tasks[i].completed = tasks[i + 1].completed; } (*numTasks)--; printf("Task deleted successfully.\n"); } else { printf("Invalid task index.\n"); } } int main() { struct Task tasks[100]; int numTasks = 0; int choice; int taskIndex; do { printf("\nTo-Do List\n"); printf("1. Add Task\n"); printf("2. Display Tasks\n"); printf("3. Mark Task as Completed\n"); printf("4. Delete Task\n"); printf("5. Exit\n"); printf("Enter your choice: "); scanf("%d", &choice); switch (choice) { case 1: addTask(tasks, &numTasks); break; case 2: displayTasks(tasks, numTasks); break; case 3: printf("Enter task index to mark as completed: "); scanf("%d", &taskIndex); markCompleted(tasks, numTasks, taskIndex); break; case 4: printf("Enter task index to delete: "); scanf("%d", &taskIndex); deleteTask(tasks, &numTasks, taskIndex); break; case 5: printf("Exiting...\n"); break; default: printf("Invalid choice.\n"); } } while (choice != 5); return 0; }
Conclusion
So here we learned about how the To-Do List in C Language : In This we learn about the major concepts of C Language like, Functions, loops, If Statements…