Command Line Menu

I wanted to post this simply because it working on it brought my a dry nostalgic joy. When I was young, 8-9, my parents got a old computer. All I remember was its screen was orange and black; having a Hercules graphics card. I quickly learned to get around from the command prompt. But I was always thrilled to run into menu driven program. It was like going to a fancy restaurant of abstractness. Anyway, when I wanted my code to slow down a bit and branch directions based upon user input, a command menu was a natural choice.

A Break from the LPC1114 Uploader

I thought I'd take some time away from coding my LPC1114 Uploader and verbally process a few things I've learned. As always, feel free to critique any of it; it'll only serve to make my code more robust in the end.

This post will be a series of post leading up to the large post I'll make on writing the uploader. All posts will rely on the GCC compiler.

Setting Up the GCC Compiler

I setup a C environment as basic I could. There may be easier ways to go about this, but I wanted to use GCC to compile.

6 and 54: This is the beginning and the end of the do-while loop. Basically, the do-while is a fancy loop which says to do everything in the brackets over and over, until the boolean variable is met (line 54).

9: I put a function in to paint my menu header. This allows me easily change the header for a menu. It also makes the menu code easier to read.

12-20: We print the menu options for the user.

22: scanf is a tricky command. It pauses and waits for the user to input followed by 'n'. It takes a variable respective to the data type you want to get from the user. Here, we are hoping the user enters one or two numbers. These are put into the string provided.

23: We use the atoi function to take a string and turn it into an integer. This value we store in the integer int_choice.

25: The beginning of the switch-statement which does the real work for us. It test the int_choice value you against predefined values (1-9 here). If the one of the values is equal, it executes the code found there before breaking from the switch-statement.

51: If a number besides 1-9 is entered the default will be true. Let's gripe at the user for selecting a bad number.

And that's it. You simply put the functions you want to be called in the appropriate values in the switch-statement. Because of the do-while loop, once a selection has been made and executed, the menu will be displayed again.

You can also make limit a selection to showing by doing something like the following,

Here, option "3. Connect Device" doesn't show until option "2. Device List" is run. On line 34 the connect_device() function sets the connected_flag variable to true if the function was successful. Then, after the break is hit and the menu is repainted the option "3. Connect Device" will show. Also, '3' will become a valid user selection.

A Submenu

One last note. If you want to make a sub-menu, you simply use the same code as above, just take the do-while loop out. This states you only want the sub-menu to run once, the return to the main menu.

We try our utmost best to provide our clients with unique and customize development & design. We create responsive design by HTML & CSS3. Our created web sites are of customize CMS which allows any user or admin to administrate it with comfort..

We are having feedback from our customers which inspire us to create android apps of multi leveled versions. Since clients’ positive responses we are trying to offer the best android apps to the market.

Since our inception of our company we try to give our best for the clients and for their satisfaction and clients also give us positive responses which inspire us to arrange new events for those who are interested in IT sectors.

We try our utmost best to provide our clients with unique and customize development & design. We create responsive design by HTML & CSS3. Our created web sites are of customize CMS which allows any user or admin to administrate it with comfort.

We are having feedback from our customers which inspire us to create android apps of multi leveled versions. Since clients’ positive responses we are trying to offer the best android apps to the market.

Since our inception of our company we try to give our best for the clients and for their satisfaction and clients also give us positive responses which inspire us to arrange new events for those who are interested in IT sectors.