Quick Start Guide
This guide will walk you through creating your first interactive console menu using EasyMenu. In just a few minutes, you'll have a fully functional menu system running in your application.
Your First Menu
Let's create a simple menu with a few options. This example demonstrates the core functionality of EasyMenu.
Step 1: Include the Header
First, include the EasyMenu header in your source file:
#include <iostream>
#include <EasyMenu.h>
Step 2: Create a Menu
Create an EasyMenu object with a title for your menu:
EasyMenu menu("Main Menu");
Step 3: Add Menu Items
Use the AddItem() method to add options to your menu:
menu.AddItem("New Game");
menu.AddItem("Load Game");
menu.AddItem("Settings");
menu.AddItem("Exit");
Step 4: Display the Menu and Get User Selection
Call RunMenu() to display the menu and capture the user's choice:
unsigned int choice = menu.RunMenu();
The RunMenu() method returns the 1-based index of the selected item. If the menu is empty or cannot be navigated, it returns 0.
Complete Example
#include <iostream>
#include <EasyMenu.h>
int main() {
// Create a menu with a title
EasyMenu menu("Main Menu");
// Add menu items
menu.AddItem("New Game");
menu.AddItem("Load Game");
menu.AddItem("Settings");
menu.AddItem("Exit");
// Display the menu and get the user's choice
unsigned int choice = menu.RunMenu();
// Handle the user's selection
if (choice == 0) {
std::cout << "No selection made." << std::endl;
} else {
std::cout << "You selected option: " << choice << std::endl;
}
return 0;
}
Understanding Navigation
When your menu runs, users can navigate using:
- Up Arrow - Move to the previous item
- Down Arrow - Move to the next item
- Enter - Select the highlighted item
The currently highlighted item is visually distinguished (typically with inverted colors or highlighting).
Handling Menu Selections
The typical pattern for handling menu selections is to use a switch statement or if-else chain:
unsigned int choice = menu.RunMenu();
switch (choice) {
case 1:
std::cout << "Starting new game..." << std::endl;
break;
case 2:
std::cout << "Loading saved game..." << std::endl;
break;
case 3:
std::cout << "Opening settings..." << std::endl;
break;
case 4:
std::cout << "Goodbye!" << std::endl;
break;
default:
std::cout << "Invalid selection." << std::endl;
}
Creating a Menu Loop
For applications that need to repeatedly show a menu until the user chooses to exit:
#include <iostream>
#include <EasyMenu.h>
int main() {
EasyMenu menu("Application Menu");
menu.AddItem("Perform action");
menu.AddItem("View data");
menu.AddItem("Exit");
bool running = true;
while (running) {
unsigned int choice = menu.RunMenu();
switch (choice) {
case 1:
std::cout << "Action performed!" << std::endl;
std::cin.get(); // Wait for user
break;
case 2:
std::cout << "Displaying data..." << std::endl;
std::cin.get();
break;
case 3:
running = false;
std::cout << "Exiting..." << std::endl;
break;
default:
running = false;
}
}
return 0;
}
Customizing Item Visibility and Activity
You can control whether items are visible and whether they can be selected:
EasyMenu menu("Game Menu");
menu.AddItem("Continue", true, true); // Active and visible
menu.AddItem("New Game", true, true); // Active and visible
menu.AddItem("Load Game", false, true); // Visible but inactive (grayed out)
menu.AddItem("Debug Mode", true, false); // Active but hidden
unsigned int choice = menu.RunMenu();
In this example:
- "Continue" and "New Game" can be selected normally
- "Load Game" is visible but cannot be selected (useful for disabled features)
- "Debug Mode" is hidden from the user entirely
Starting from a Specific Item
You can specify which item should be highlighted when the menu first appears:
EasyMenu menu("Main Menu");
menu.AddItem("Option 1");
menu.AddItem("Option 2");
menu.AddItem("Option 3");
// Start with the second item highlighted
unsigned int choice = menu.RunMenu(2);
This is useful when you want to guide the user's attention to a specific option.
Modifying Menu Items
You can update or remove items after creating the menu:
EasyMenu menu("Dynamic Menu");
menu.AddItem("Item 1");
menu.AddItem("Item 2");
menu.AddItem("Item 3");
// Update item 2 (1-based index)
menu.UpdateItem(2, "Updated Item 2");
// Remove item 3
menu.RemoveItem(3);
// Add a new item
menu.AddItem("New Item");
unsigned int choice = menu.RunMenu();
Next Steps
Now that you've created your first menu, you can:
- Learn about key concepts behind EasyMenu
- Explore the full API to discover advanced features
- View more examples for common use cases
- Read the class reference for detailed method documentation