Todo List in C without file I/O and DMA.

 Main.c

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

#define MAX_NAME 25
#define MAX_TASKS 50

typedef struct task{
    char name[MAX_NAME];
    short int Status;
}task;

void addTask(task* tasks,int* totalTasks);
void removeTask(int* totalTasks,task* tasks);
void viewTask(int* totalTasks,task* tasks);


int main(){
    task tasks[MAX_TASKS];
    int totalTasks = 0;
    int input;
    while(1){
       
        printf("Welcome to Todo-List Manager\n");
        printf("Enter the corresponding number to the task you want to do\n");
        printf("1.Add a new task\n");
        printf("2.View all your tasks\n");
        printf("3.Remove a task\n");
        printf("4.Exit the program\n");
        scanf("%d",&input);
       
        switch(input){
            case 1:
                addTask(tasks,&totalTasks);
                break;
            case 2:
                viewTask(&totalTasks,tasks);
                break;
            case 3:
                removeTask(&totalTasks,tasks);
                break;
            case 4:
                return 0;
                break;
            default:
                printf("Please enter a valid number\n");
                break;               
        }

    }
}

void addTask(struct task* tasks,int* totalTasks){

    if (*totalTasks >= MAX_TASKS ){
        printf("The task list is full please remove some tasks\n");
        return;
    }
    
    char name[MAX_NAME];
    task ttask;
    
    printf("Enter the name of the task\n");
    scanf("%s",name);
    
    strcpy(ttask.name,name);
    ttask.Status = 0;
    tasks[*totalTasks] = ttask;
    *totalTasks += 1;
    
    return;


}

void removeTask(int* totalTasks,struct task* tasks){

    if (*totalTasks <= 0){
        printf("The list is empty\n");
        return;
    }
    
    int taskIndex;
    printf("Which task do you want to remove(number)");
    scanf("%d",&taskIndex);
    
    if (taskIndex > *totalTasks || taskIndex <= 0){
        printf("Please enter a valid number\n");
        return;
    }
    else{
        tasks[taskIndex -1 ].Status = -1;
        *totalTasks -= 1;
        return;
    }
       
}

void viewTask(int* totalTasks,struct task* tasks){

    if (*totalTasks <= 0){
        printf("Your task list is empty\n");
        return;
    }
    
    for(int i = 0;i < *totalTasks;i++){
    
        if(tasks[i].Status == -1){
            continue;
        }
        else{
            printf("%d. %s - %s\n",i+1,tasks[i].name, (tasks[i].Status==1) ? "Complete" : "Incomplete");
        }
    }
    return;
}



 

Comments

Popular posts from this blog

Time Calculator

Time Calculator/src/timef.h (Time Calculator)

Time Calculator/src/timec.h (Time Calculator)