r/C_Programming • u/mwiseca • Sep 21 '26
How i now build a menu program in C.
The example below uses a struct to map strings to functions.
The functions in the example are useless are used for demo purposes.
The strlen if statement was so that users wont have to press enter twice when using the
flush function. Its probably not the best way of doing it but its simple.
the values are function pointers.
Nested if-else chains and switch blocks are the default way to build console menus in C.
But I now do it different.
I find this easier than using 2 arrays and if else statements.
I also like that the for loop shows the amount of functions.
Credit note: I didn't write this code by hand—it was entirely made by my Cstrings program.
#include <stdio.h>
#include <string.h>
void flush(){
int clear;
while ((clear = getc(stdin)) != '\n' && clear != EOF) {
}
}
void array(){
printf("make a array.\n");
}
void string(){
printf("Make a string.\n");
}
void ifStatement(){
printf("Make a if statement.\n");
}
void elseIf(){
printf("Make a else if statement.\n");
}
struct menu {
const char* keys;
void(*values)(void);
};
int main() {
struct menu m1[] = {
{"a", array},
{"b", string},
{"c", ifStatement},
{"d", elseIf},
};
char select [25];
printf("Enter a,b,c or d x to exit.\n");
while(1) {
if(fgets(select,sizeof(select),stdin) == NULL) {
clearerr(stdin);
printf("\nInvalid input.\n\n");
continue;
}
select[strcspn(select,"\n")]=0;
if(strlen(select) >= 24) {
flush();
}
if(strcmp(select,"x")==0) {
break;
}
int index = -1;
for(int i = 0;i<4;i++) {
if(strcmp(select, m1[i].keys) == 0) {
index = i;
}
}
if(index == -1) {
printf("\nKey error: Enter a,b,c or d only.\n\n");
continue;
}
m1[index].values();
}
return 0;
}
3
u/FancySpaceGoat Sep 21 '26 edited Sep 21 '26
Yes. An associative container (list of key-value pairs) is a good fit for this.
If you wanted to get pedantic about it, you "could" go for something that provides faster lookups (binary search or hashmap) or something more flexible (a dynamic table of commands, or support for arguments). But frankly, I wouldn't bother until there's a need for it.
If it works well enough for you, and is easy for you to maintain, then good enough. Spend your time on the actual functionality of what you are building.
3
u/flyingron Sep 21 '26
If you're really going to use single char commands for everything, there's no need to make them strings. It would be faster and more compact to use just a char and simple equality tests.
1
u/TituxDev Sep 24 '26
You can avoid the struct, create a single function pointer array. Index can reemplace character selector using <input>-'a' Don't require string, just getchar or getc
Sorry, My English isnt good
3
u/Syntax-Tactics Sep 23 '26
while (1) break; I'm done looking at it.
2
u/Ancient-Opinion9642 Sep 22 '26
Go dig up the September, 1988, issue of Dr. Dobbs Journal from some Wayback machine. Al Stevens published code for C to do what you are asking. It is a place to start, but you will have to provide your own primitives. The published primitives are for DOS. The entire code for the project is available in the next 12 months (or so).
A couple of years later, the code for the same project was published in C++.
Sadly, I remember typing the code in!!!
1
u/Reasonable-Pay-8771 Sep 22 '26
Nice. I'd be tempted to replace the struct with an X Macro and unroll the loop into a switch or if/else chain using the Macro.