Example program (user_menu_code.c) for presenting a text menu to the user and getting a valid option from the user.
From: Amit
Date: Tue Mar 04 2025 - 06:20:48 EST
--------------------------
user_menu_code.c
--------------------------
/*
* License: This file has been released under GNU General Public License,
* version 2 (GPLv2).
*
* The license details can be found here:
* https://www.gnu.org/licenses/old-licenses/gpl-2.0.en.html
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#define NUM_TRUE 1
#define NUM_FALSE 0
// function prototypes for gcc flag -Werror-implicit-function-declaration
char *get_input_from_stdin_and_discard_extra_characters(char *str, long size);
int is_str_a_number(const char *str);
void print_menu(void);
int print_menu_and_get_valid_user_option(void);
void process_user_option(int option);
void __func(int option);
char *get_input_from_stdin_and_discard_extra_characters(char *str, long size)
{
int c = 0;
long i = 0;
// If 'size' is 0 then this function will discard all input and return NULL.
// No need to check 'str' if 'size' is 0.
if (size == 0) {
// discard all input
while ((c = getchar()) && (c != '\n') && (c != EOF));
return NULL;
}
if (str == NULL)
return NULL;
if (size < 0)
return NULL;
for (i = 0; i < (size - 1); i = i + 1) {
c = getchar();
if ((c == '\n') || (c == EOF)) {
str[i] = 0;
return str;
}
str[i] = (char)(c);
} // end of for loop
str[i] = 0;
// discard rest of input
while ((c = getchar()) && (c != '\n') && (c != EOF));
return str;
} // end of function get_input_from_stdin_and_discard_extra_characters()
int is_str_a_number(const char *str)
{
char c = '=';
if (str == NULL) {
return NUM_FALSE;
}
if (str[0] == '\0') { // empty string
return NUM_FALSE;
}
while ((c = *str)) {
if ((c < '0') || (c > '9')) {
return NUM_FALSE;
}
str++;
}
return NUM_TRUE;
} // end of function is_str_a_number()
void print_menu(void)
{
printf("\n\n");
printf("----\n");
printf("Menu\n");
printf("----\n");
printf("\n");
printf("1. Option 1\n");
printf("2. Option 2\n");
printf("3. Option 3\n");
printf("4. Exit\n");
return;
} // end of function print_menu()
int print_menu_and_get_valid_user_option(void)
{
#define MIN_USER_OPTION 1
#define MAX_USER_OPTION 4
#define OPTION_SIZE 8
char option_str[OPTION_SIZE] = {0};
int option = -1;
while (1) {
print_menu();
printf("\n");
while (1) {
printf("Please enter a valid option (%d - %d)"
" (spaces not allowed): ", MIN_USER_OPTION, MAX_USER_OPTION);
get_input_from_stdin_and_discard_extra_characters(option_str,
OPTION_SIZE);
if (is_str_a_number(option_str) != NUM_TRUE) {
continue;
}
option = atoi(option_str);
if ((option < MIN_USER_OPTION) || (option > MAX_USER_OPTION)) {
continue;
}
printf("\n");
printf("Option number given by the user is: %d\n", option);
printf("\n");
printf("Do you want to proceed (Enter only 'n' (without the quotes)"
" for not proceeding, enter any thing else for proceeding):"
" ");
get_input_from_stdin_and_discard_extra_characters(option_str,
OPTION_SIZE);
if (strcmp(option_str, "n") != 0) {
return option;
}
break;
} // end of inner while(1) loop
} // end of outer while(1) loop
// Unreachable code. Program execution should not reach here.
// Exit the program.
printf("\n");
printf("Line %d: Some BUG in the program. Exiting..\n", __LINE__);
printf("\n");
exit(1);
} // end of function print_menu_and_get_valid_user_option()
void process_user_option(int option)
{
switch (option) {
case 1: __func(option);
break;
case 2: __func(option);
break;
case 3: __func(option);
break;
case 4: printf("\n");
printf("**THE USER GAVE OPTION NUMBER %d**\n", option);
printf("\n");
printf("Exiting..\n");
printf("\n");
exit(0);
default: printf("\n");
printf("Line %d: Some BUG in the program. Exiting..\n",
__LINE__);
printf("\n");
exit(1);
} // end of switch statement
return;
} // end of function process_user_option()
void __func(int option)
{
printf("\n");
printf("**THE USER GAVE OPTION NUMBER %d**\n", option);
return;
} // end of function __func()
int main(void)
{
int option = -1;
while (1) {
option = print_menu_and_get_valid_user_option();
process_user_option(option);
} // end of while loop
} // end of function main()