#includeint main() { int choice; while (true) { std::cout << "Menu Options:\n"; std::cout << "1. Option 1\n"; std::cout << "2. Option 2\n"; std::cout << "3. Quit\n"; std::cout << "Enter your choice: "; std::cin >> choice; switch (choice) { case 1: // Handle Option 1 break; case 2: // Handle Option 2 break; case 3: // Quit return 0; default: std::cout << "Invalid choice, please try again.\n"; } } return 0; }
#includeint main() { initscr(); cbreak(); noecho(); keypad(stdscr, true); int choice = 1; while (true) { clear(); printw("Menu Options:\n"); printw("%sOption 1%s\n", choice == 1 ? "-> " : " ", choice == 1 ? " <-" : ""); printw("%sOption 2%s\n", choice == 2 ? "-> " : " ", choice == 2 ? " <-" : ""); printw("%sQuit%s\n", choice == 3 ? "-> " : " ", choice == 3 ? " <-" : ""); refresh(); int input = getch(); switch (input) { case KEY_UP: if (choice > 1) choice--; break; case KEY_DOWN: if (choice < 3) choice++; break; case '\n': switch (choice) { case 1: // Handle Option 1 break; case 2: // Handle Option 2 break; case 3: endwin(); return 0; } } } return 0; }
#includePackage/library: Standard C++ library (for #1), ncurses library (for #2), and Qt framework (for #3).#include #include #include #include int main(int argc, char *argv[]) { QApplication app(argc, argv); QMainWindow window; QMenuBar *menu = window.menuBar(); QMenu *fileMenu = menu->addMenu("File"); QAction *quitAction = fileMenu->addAction("Quit"); QObject::connect(quitAction, &QAction::triggered, &app, &QApplication::quit); window.show(); return app.exec(); }