Create the Simple To Do List in C Language with free codes

Index

Introduction

Here we are going to create a simple To Do List in C Language which allow to user to create, read, update, and delete the tasks from the to-do items.

Pattern for Creating To Do List

The Pattern of the logic of the To Do List is based on managing arrays of structures “ToDoItem”. Each structure holds task string and a completion status. Basic Operation such as add, update, delete, and mark as completed are performed using array manipulations and string operations.

To Do List - E-Books

Project Codes

Here is the complete Project code of To Do List in C,

#include <stdio.h>
#include <string.h>

#define MAX_TODOS 100
#define MAX_LEN 100

typedef struct {
    char task[MAX_LEN];
    int completed;
} Todo;

void addTodo(Todo todos[], int *count) {
    if (*count >= MAX_TODOS) {
        printf("To-do list is full.\n");
        return;
    }

    printf("Enter the task: ");
    getchar(); // To consume the newline character left by the previous input
    fgets(todos[*count].task, MAX_LEN, stdin);
    todos[*count].task[strcspn(todos[*count].task, "\n")] = '\0'; // Remove the newline character
    todos[*count].completed = 0;
    (*count)++;
    printf("Task added.\n");
}

void displayTodos(Todo todos[], int count) {
    if (count == 0) {
        printf("No tasks in the to-do list.\n");
        return;
    }

    printf("To-Do List:\n");
    for (int i = 0; i < count; i++) {
        printf("%d. %s [%s]\n", i + 1, todos[i].task, todos[i].completed ? "Completed" : "Not Completed");
    }
}

void updateTodo(Todo todos[], int count) {
    int index;
    printf("Enter the task number to update: ");
    scanf("%d", &index);

    if (index < 1 || index > count) {
        printf("Invalid task number.\n");
        return;
    }

    printf("Enter the new task: ");
    getchar(); // To consume the newline character left by the previous input
    fgets(todos[index - 1].task, MAX_LEN, stdin);
    todos[index - 1].task[strcspn(todos[index - 1].task, "\n")] = '\0'; // Remove the newline character
    printf("Task updated.\n");
}

void deleteTodo(Todo todos[], int *count) {
    int index;
    printf("Enter the task number to delete: ");
    scanf("%d", &index);

    if (index < 1 || index > *count) {
        printf("Invalid task number.\n");
        return;
    }

    for (int i = index - 1; i < *count - 1; i++) {
        todos[i] = todos[i + 1];
    }
    (*count)--;
    printf("Task deleted.\n");
}

void markComplete(Todo todos[], int count) {
    int index;
    printf("Enter the task number to mark as completed: ");
    scanf("%d", &index);

    if (index < 1 || index > count) {
        printf("Invalid task number.\n");
        return;
    }

    todos[index - 1].completed = 1;
    printf("Task marked as completed.\n");
}

int main() {
    Todo todos[MAX_TODOS];
    int count = 0;
    int choice;

    while (1) {
        printf("\nTo-Do List Menu:\n");
        printf("1. Add Task\n");
        printf("2. Display Tasks\n");
        printf("3. Update Task\n");
        printf("4. Delete Task\n");
        printf("5. Mark Task as Completed\n");
        printf("6. Exit\n");
        printf("Enter your choice: ");
        scanf("%d", &choice);

        switch (choice) {
            case 1:
                addTodo(todos, &count);
                break;
            case 2:
                displayTodos(todos, count);
                break;
            case 3:
                updateTodo(todos, count);
                break;
            case 4:
                deleteTodo(todos, &count);
                break;
            case 5:
                markComplete(todos, count);
                break;
            case 6:
                printf("Exiting program.\n");
                return 0;
            default:
                printf("Invalid choice. Please try again.\n");
        }
    }
}

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top