20 Dec Example

 #include <stdio.h>

#include <string.h>


#define MAX_TASKS 100

#define MAX_TASK_LEN 100


void addTask(char tasks[][MAX_TASK_LEN], int *taskCount) {

    if (*taskCount >= MAX_TASKS) {

        printf("\nTask list is full!\n");

        return;

    }


    printf("\nEnter the task: ");

    getchar(); // to consume leftover newline character

    fgets(tasks[*taskCount], MAX_TASK_LEN, stdin);

    tasks[*taskCount][strcspn(tasks[*taskCount], "\n")] = 0; // Remove newline

    (*taskCount)++;


    printf("Task added successfully!\n");

}


void editTask(char tasks[][MAX_TASK_LEN], int taskCount) {

    if (taskCount == 0) {

        printf("\nNo tasks to edit!\n");

        return;

    }


    int taskNum;

    printf("\nEnter the task number to edit (1 to %d): ", taskCount);

    scanf("%d", &taskNum);


    if (taskNum < 1 || taskNum > taskCount) {

        printf("Invalid task number!\n");

        return;

    }


    printf("\nEnter the new task: ");

    getchar(); // to consume leftover newline character

    fgets(tasks[taskNum - 1], MAX_TASK_LEN, stdin);

    tasks[taskNum - 1][strcspn(tasks[taskNum - 1], "\n")] = 0; // Remove newline


    printf("Task updated successfully!\n");

}


void deleteTask(char tasks[][MAX_TASK_LEN], int *taskCount) {

    if (*taskCount == 0) {

        printf("\nNo tasks to delete!\n");

        return;

    }


    int taskNum;

    printf("\nEnter the task number to delete (1 to %d): ", *taskCount);

    scanf("%d", &taskNum);


    if (taskNum < 1 || taskNum > *taskCount) {

        printf("Invalid task number!\n");

        return;

    }


    for (int i = taskNum - 1; i < *taskCount - 1; i++) {

        strcpy(tasks[i], tasks[i + 1]);

    }


    (*taskCount)--;

    printf("Task deleted successfully!\n");

}


void displayTasks(char tasks[][MAX_TASK_LEN], int taskCount) {

    if (taskCount == 0) {

        printf("\nNo tasks to display!\n");

        return;

    }


    printf("\nYour Tasks:\n");

    for (int i = 0; i < taskCount; i++) {

        printf("%d. %s\n", i + 1, tasks[i]);

    }

}


int main() {

    char tasks[MAX_TASKS][MAX_TASK_LEN];

    int taskCount = 0;

    int choice;


    do {

        printf("\nTo-Do List Menu:\n");

        printf("1. Add Task\n");

        printf("2. Edit Task\n");

        printf("3. Delete Task\n");

        printf("4. Display Tasks\n");

        printf("5. Exit\n");

        printf("Enter your choice: ");

        scanf("%d", &choice);


        switch (choice) {

            case 1:

                addTask(tasks, &taskCount);

                break;

            case 2:

                editTask(tasks, taskCount);

                break;

            case 3:

                deleteTask(tasks, &taskCount);

                break;

            case 4:

                displayTasks(tasks, taskCount);

                break;

            case 5:

                printf("\nExiting...\n");

                break;

            default:

                printf("\nInvalid choice! Please try again.\n");

        }

    } while (choice != 5);


    return 0;

}


Comments

Popular posts from this blog

Programming Notes by Atul Kalukhe