Project design
- C++ course
- Beginner
- Lesson 112/116
- 6 min
Design the skeleton of the beginner final mini app.
Project: Task list
In this project you will build a small task list application as the capstone of the beginner level. Each lesson adds a piece, and the final lesson brings everything together. The first lesson sets up the program skeleton and menu.
The app will show a menu: 1 - Add, 2 - List, 3 - Exit. In this step you only define a printMenu function that prints the menu and call it from main. Later lessons will add task adding, listing, and error handling.
Calling the printMenu function
After defining a function, you call it inside main. The call is the function name followed by empty parentheses: printMenu();. When the program runs, the menu is printed.
Defining a function and calling it are different. Definition tells what the function does; a call runs it. In this project you define printMenu once and can call it many times, which is the main advantage of functions.
Practice Set up the menu skeleton
Define a function called printMenu with three cout calls: "1 - Add", "2 - List", "3 - Exit" (each with endl). Then call printMenu(); inside main.
Starter code
#include <iostream>
using namespace std;
// Define the printMenu function
int main() {
// call the function
return 0;
}Sample solution
#include <iostream>
using namespace std;
void printMenu() {
cout << "1 - Add" << endl;
cout << "2 - List" << endl;
cout << "3 - Exit" << endl;
}
int main() {
printMenu();
return 0;
}Expected output
1 - Add 2 - List 3 - Exit
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Why is it useful to put the menu in a separate function?
- To make code reusable and organised Correct answer
- Because it runs faster
- Because it is required
- Because it uses less memory
Explanation: Functions make code reusable and organised.
2 Fill in the blank: menu line
void printMenu() {
cout << "1 - Add" << endl;
cout << "2 - List" << endl;
____;
}
void printMenu() {
cout << "1 - Add" << endl;
cout << "2 - List" << endl;
cout << "3 - Exit" << endl;
}
Explanation: The exit line is also printed with endl.
3 Match each menu item with its description
| Term | Meaning |
|---|---|
1 - Add |
add a task |
2 - List |
show tasks |
3 - Exit |
quit the program |