Putting it together
- C++ course
- Beginner
- Lesson 116/116
- 6 min
Combine all parts into a working final app.
Combining the menu with switch
In the final lesson we will combine all parts and simulate a menu selection. The switch statement runs the matching branch based on the choice variable; each branch corresponds to a menu option.
In this lesson we will add a switch (choice) structure after the printMenu(); call. case 1: will be add, case 2: list, case 3: exit. This shows the entire application flow in one place.
Flow of the switch statement
The switch statement takes an integer value and jumps to the matching case branch. Writing break; at the end of each branch prevents other branches from running. The default: branch is a fallback for values that match no case.
switch (choice) {
case 1: addTask(...); break;
case 2: listTasks(...); break;
case 3: return 0;
default: cout << "invalid";
}Here when choice is 1, 2, or 3, the matching branch runs. break exits the switch; return 0 ends the program directly. This structure is the basis of menu applications.
Practice Combine the menu with switch
After printMenu();, add vector<Task> tasks; and addTask(tasks, "Homework", "pending");. Then simulate the choice with int choice = 2;. Open switch (choice); in case 1: write addTask(tasks, "Sport", "pending"); break;; in case 2: write listTasks(tasks); break;; in case 3: write cout << "Exit" << endl; break;. In the default: branch add cout << "Invalid" << endl;.
Starter code
#include <iostream>
#include <string>
#include <vector>
#include <stdexcept>
using namespace std;
struct Task {
string title;
string status;
};
class InvalidTaskException : public std::exception {
public:
const char* what() const noexcept override {
return "invalid task";
}
};
void printMenu() {
cout << "1 - Add" << endl;
cout << "2 - List" << endl;
cout << "3 - Exit" << endl;
}
void addTask(vector<Task>& tasks, string title, string status) {
if (title.empty()) throw InvalidTaskException();
Task t;
t.title = title;
t.status = status;
tasks.push_back(t);
}
void listTasks(const vector<Task>& tasks) {
for (Task t : tasks) {
cout << t.title << " - " << t.status << endl;
}
}
int main() {
printMenu();
// tasks and addTask call, combine the menu with switch
return 0;
}Sample solution
#include <iostream>
#include <string>
#include <vector>
#include <stdexcept>
using namespace std;
struct Task {
string title;
string status;
};
class InvalidTaskException : public std::exception {
public:
const char* what() const noexcept override {
return "invalid task";
}
};
void printMenu() {
cout << "1 - Add" << endl;
cout << "2 - List" << endl;
cout << "3 - Exit" << endl;
}
void addTask(vector<Task>& tasks, string title, string status) {
if (title.empty()) throw InvalidTaskException();
Task t;
t.title = title;
t.status = status;
tasks.push_back(t);
}
void listTasks(const vector<Task>& tasks) {
for (Task t : tasks) {
cout << t.title << " - " << t.status << endl;
}
}
int main() {
printMenu();
vector<Task> tasks;
addTask(tasks, "Homework", "pending");
int choice = 2;
switch (choice) {
case 1:
addTask(tasks, "Sport", "pending");
break;
case 2:
listTasks(tasks);
break;
case 3:
cout << "Exit" << endl;
break;
default:
cout << "Invalid" << endl;
}
return 0;
}Expected output
1 - Add 2 - List 3 - Exit Homework - pending
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Match each menu item with its branch
| Term | Meaning |
|---|---|
1 - Add |
case 1 |
2 - List |
case 2 |
3 - Exit |
case 3 |
2 What does case 2 call in switch (choice)?
- listTasks Correct answer
- addTask
- printMenu
- throw
Explanation: case 2 calls listTasks for listing.
3 Fill in the blank: break call
case 1:
addTask(tasks, "Homework", "pending");
____;
break;
case 1:
addTask(tasks, "Homework", "pending");
break;
break;
Explanation: Write break; it is used to exit a switch branch.