CodeBlanks Logo Icon CodeBlanks

Putting it together

Build a working CLI app with a menu loop.

Putting it together

In the final step we combine all pieces into a complete to-do CLI. Three functions (add_task, list_tasks, complete_task) and the struct Task array work together.

In this lesson we add a third task and complete the flow. This way the add, complete, list flow works end to end.

The menu loop idea

In a real CLI app the user picks from a menu: add task, list, complete or exit. A menu loop can be built with while and switch.

while (choice != 0) {
    switch (choice) {
        case 1: count = add_task(tasks, count, title); break;
        case 2: list_tasks(tasks, count); break;
        case 3: complete_task(tasks, id); break;
    }
}

In this lesson we use a fixed flow instead of a menu: add, complete, add again, list.

Practice Add the third task and complete the flow

In the code below add the line count = add_task(tasks, count, "Reading"); after the complete_task(tasks, 1); call. This runs the full three-task flow: add, add, complete, add again, list. Output should be 1 Shopping 1, 2 Sport 0 and 3 Reading 0.

Starter code

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

struct Task {
    int id;
    char title[50];
    int done;
};

int add_task(struct Task tasks[], int count, const char *title) {
    tasks[count].id = count + 1;
    strcpy(tasks[count].title, title);
    tasks[count].done = 0;
    return count + 1;
}

void list_tasks(struct Task tasks[], int count) {
    for (int i = 0; i < count; i++) {
        printf("%d %s %d\n", tasks[i].id, tasks[i].title, tasks[i].done);
    }
}

void complete_task(struct Task tasks[], int id) {
    tasks[id - 1].done = 1;
}

int main(void) {
    struct Task tasks[10];
    int count = 0;
    count = add_task(tasks, count, "Shopping");
    count = add_task(tasks, count, "Sport");
    complete_task(tasks, 1);
    list_tasks(tasks, count);
    return 0;
}

Sample solution

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

struct Task {
    int id;
    char title[50];
    int done;
};

int add_task(struct Task tasks[], int count, const char *title) {
    tasks[count].id = count + 1;
    strcpy(tasks[count].title, title);
    tasks[count].done = 0;
    return count + 1;
}

void list_tasks(struct Task tasks[], int count) {
    for (int i = 0; i < count; i++) {
        printf("%d %s %d\n", tasks[i].id, tasks[i].title, tasks[i].done);
    }
}

void complete_task(struct Task tasks[], int id) {
    tasks[id - 1].done = 1;
}

int main(void) {
    struct Task tasks[10];
    int count = 0;
    count = add_task(tasks, count, "Shopping");
    count = add_task(tasks, count, "Sport");
    complete_task(tasks, 1);
    count = add_task(tasks, count, "Reading");
    list_tasks(tasks, count);
    return 0;
}

Expected output

1 Shopping 1
2 Sport 0
3 Reading 0

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 Add the third task

Question
count = add_task(tasks, count, ____);  /* add third task */
Answer
count = add_task(tasks, count, "Reading");  /* add third task */

Explanation: You added the third task with title Reading.

2 Match functions

Term Meaning
add_task adding tasks
list_tasks listing
complete_task completing
while menu loop

3 What is the best structure for a CLI menu loop?

  • A struct definition
  • Just printf
  • A single if
  • A while loop with switch Correct answer

Explanation: A while loop gives repetition, switch gives selection.

All lessons in Beginner