This C program provides a simple console-based menu system with navigation and selection capabilities. It allows users to navigate through menu options using numeric keys and select an option using the ENTER key. The selected option is then returned for further processing.
- Displays a formatted menu with a customizable header.
- Highlights the selected menu option in purple.
- Allows navigation using numeric keys.
- Detects and processes the
ENTERkey to confirm a selection. - Clears the screen upon selection for a seamless user experience.
menu.c: Contains the implementation of the menu system.menu.h: (Not provided, but expected to contain function prototypes and necessary macros.)
int menu(const char header[], const char menuElements[][50], const char endPhrase[], int numOfElements);
This function displays a menu and allows the user to navigate and select an option.
Parameters:
header[]: The title/header of the menu.menuElements[][50]: Array of menu options (strings).endPhrase[]: A closing phrase to be displayed when exiting the menu.numOfElements: Number of menu options available.
Returns:
- The selected menu option index (1-based).
This function captures a single character input without requiring the user to press ENTER.
Returns:
- The numeric value of the key pressed.
To compile the program, use:
gcc menu.c -o menuTo execute:
./menu#include "menu.h"
int main() {
const char header[] = "Main Menu";
const char menuElements[][50] = {"Option 1", "Option 2", "Exit"};
const char endPhrase[] = "Goodbye!";
int numOfElements = 3;
int choice = menu(header, menuElements, endPhrase, numOfElements);
printf("You selected option: %d\n", choice);
//clear buffer when additional input is used (loop)
//while ((getchar()) != '\n');
return 0;
}- Standard C libraries:
<stdio.h>,<termios.h>,<unistd.h>,<stdlib.h>,<string.h>.
- The
system("clear")command is used to refresh the console, which may need adjustment for non-UNIX systems. - The function
getch()modifies terminal attributes to allow single-character input.
Created by HalfAsleepDev
- 1.0 (Last update: February 11, 2025)
- 2.0 (Last update: February 13, 2025)
- BUG FIXES Arrow key navigation, Clear buffer fix
- Start working on adv-menu (Last update: February 15, 2025)
- Make the menu mostly modular
- Start working on Ver 3.0
- Seperate simple and advanced menus?
- Re-orginize files
- Create new README.md files
- Seperate basic and advenced menu types