I want to article this simply due to the fact that it functioning on it brought my a dry nostalgic joy. Once I was young, 8-9, my parents gained a old computer. All ns remember to be its display was orange and also black; having actually a Hercules graphics card. I quickly learned to get around from the command prompt. But I was constantly thrilled to run into menu pushed program. That was favor going come a sophisticated restaurant the abstractness. Anyway, once I want my password to sluggish down a bit and also branch direction based upon user input, a command menu was a organic choice.

You are watching: How to make a menu in c++

A rest from the LPC1114 Uploader

I assumed I’d take part time far from coding my LPC1114 Uploader and verbally procedure a couple of things i have learned. As always, feel totally free to critique any kind of of it; it’ll only serve to do my code an ext robust in the end.

This write-up will be a collection of post leading approximately the big post I’ll make on writing the uploader. All write-ups will count on the GCC compiler.

*
Setting increase the GCC Compiler

I setup a C atmosphere as straightforward I could. There may be easier ways come go around this, however I wanted to use GCC to compile.

To setup the environment:

1. Ns downloaded and setup MinGW32.

2. I included these includes to make the password go.


#include #include #include #include #include #include #include #include #include #include #include #include
I supplied this heat to build it:

$ gcc -o main main.c

As because that editing, I’ve yes, really grown to love Sublime text 2.

If you have issues, make sure brochure containing your documents is in her PATH atmosphere variable (I go over exactly how to add the directory to your atmosphere variables in this post).

*

How to create a Command heat Menu

There really isn’t lot to the code here. Basically, the it prints out the options you want your user come know. Then, it starts a do-while loop until the user selects an appropriate number.

Hmm. Not really much to it, not certain it deserves its very own post. But what the hell.


void main_menu()char char_choice<3>;int int_choice = 0;dosystem("cls");startScreen();printf(" ");printf("Vorpal Hoff -- main Menu: ");printf("1. Quick Connect ");printf("2. FTDI Menu ");printf("3. Open up HM-1X Menu ");printf("4. Connect LPC ");printf("5. Regime Chip ");printf("6. Erase LPC ");printf("7. Decode UUE debug file ");printf("8. Parse hex-file and also write come debug.hex ");printf("9. Exit ");scanf("%s", char_choice);int_choice = atoi(char_choice);switch (int_choice)case 1:quick_connect();break;case 2:ftdi_menu();break;case 3: HM_1X_main_menu();break;case 4:// not yet used.break;case 5:program_chip(file_name); break;case 6:// no yet used. Break;case 8:debug_hex_file(file_name); break;case 9:shut_down(); break;default:printf("Wrong choice. Enter Again"); break;while(int_choice !=99);
6 and also 54: This is the beginning and the finish of the do-while loop. Basically, the do-while is a an elaborate loop which says to do every little thing in the brackets over and over, till the boolean variable is met (line 54).

The do-while loop if the identical in impact to the monitor code,


while (true) do_work(); if (!condition) break;
8: removes the screen. This clears the command prompt; providing a clean slate to paint our menu. 9: I put a function in to paint my food selection header. This enables me easily readjust the header for a menu. It also makes the menu code less complicated to read. 12-20: We publish the menu choices for the user. 51: If a number besides 1-9 is entered the default will certainly be true. Let’s gripe at the user for selecting a bad number.And that’s it. You simply put the functions you desire to be referred to as in the ideal values in the switch-statement. Because of the do-while loop, when a selection has to be made and executed, the menu will be presented again.

You can likewise make limit a an option to mirroring by doing something favor the following,


bool connected_flag = false;dosystem("cls");printf("FTDI Menu: ");printf("1. Fast Connect ");printf("2. Device List ");if (got_list == true) // Only screen option if devices list.printf("3. Connect Device ");printf("9. Main Menu ");// gain user choice.scanf("%s", char_choice);// convert string come int because that switch statement.int_choice = atoi(char_choice);switch (int_choice)case 1:quick_connect();baud_rate = 115200;connected_flag = true;case 2:got_list = get_device_list();break;case 3:if (got_list == true) // Only display option if devices listed.connected_flag = connect_device(&baud_rate);break;case 9:main_menu(); break;default:printf("""Bad choice. Hot glue!"""); break;while(int_choice !=99);}
Here, alternative “3. Affix Device” doesn’t present until option “2. An equipment List” is run. On heat 34 the connect_device() duty sets the connected_flag change to true if the duty was successful. Then, ~ the rest is hit and the menu is repainted the choice “3. Attach Device” will show. Also, ‘3’ will end up being a valid user selection.

See more: Chart Of Color That Starts With The Letter H Art Of Colors That Start With H

A Submenu

One last note. If you desire to make a sub-menu, you just use the exact same code together above, simply take the do-while loop out. This states you just want the sub-menu to operation once, the go back to the main menu.