Check-Menus.com

c++ console menu example

by Aaron Schamberger Published 2 years ago Updated 1 year ago
image

How do I build a console menu in Visual Studio?

Building the Console Menu. The MainMenu() method should first print the menu options to the console so the user is able to make a selection. Next, it will read the user’s response and determine which option was selected. Based on the option that was selected, the program will call the appropriate method.

How to display simple menu using do-while construct in C program?

To do: Displaying a simple menu using do-while construct in C program To show: Another do-while statement C program example // Another do-while statement example #include <stdio.h> // Prototype required by standard int get_menu_choice(void); void main(void) int choice; // Call get_menu_choice() function choice = get_menu_choice();

How does a menu-driven console application work?

In short, our menu-driven console application will use a While Loop that will call a Main Menu method. The Main Menu method will print all menu options to the console and wait for the user to choose an option. Once the user selects an option, the program will execute the code block pertaining to the selected option.

What is the function of the menu class?

The example used here to demonstrate the input functions and the menu class is a simple two-level menu that allows data of different types ( char, signed int, unsigned int, double and string) to be entered and stored in a single vector.

image

Create a project

To start, create a C# application project. The project type comes with all the template files you need.

Debug the app

You've improved your basic calculator app, but your app doesn't yet handle exceptions, such as user input errors. For example, if users try to divide by zero, or enter an unexpected character, the app might stop working, return an error, or return an unexpected nonnumeric result.

Add Git source control

Now that you've created an app, you might want to add it to a Git repository. We've got you covered. Visual Studio makes that process easy with Git tools you can use directly from the IDE.

Review: Code complete

In this tutorial, you made many changes to the calculator app. The app now handles computing resources more efficiently, and handles most user input errors.

5 Custom function used in displaymenu ()

These are the 5 functions we are using to display different MENU as per user input. We can change the number of many MENUS as per need.

How does it works

Inside the Main () function we are using the switch statement to display the MENU as per user input.

Background

This article arose from Bartek’s blog regarding std::any where he asked for examples of usage. This followed his excellent series of articles on the new C++17 std::any , std::variant and std::optional features.

Put Simply

What I developed is a very simple command-line menu class and associated utility functions. These utility functions provide the easy means to obtain console input – which as every C++ programmer knows – is fraught with issues regarding stream state etc etc etc for ‘bad input’.

The Console Input Functions

These provide an easy means of obtaining different types of data from keyboard input – a string (whole line of data), a number (of different types and within optional specified ranges) and a single char (optionally restricted to a specified set of chars).

The Menu Class

As I said earlier, this is a simple console menu system. The heart of which revolves around the Menu class.

The Example

The example used here to demonstrate the input functions and the menu class is a simple two-level menu that allows data of different types ( char, signed int, unsigned int, double and string) to be entered and stored in a single vector.

Play With the Code

But, below you can also play live with it (and even work in a terminal! (sessions are scheduled to last max 60 seconds):

In conclusion

It is of course, up to the user of Menu to determine the menu structure and the type used with any<> as specified by Params. But if a quick console application is needed that uses a menu and console input, then this class and the various console input utility functions may help to reduce the required effort.

image

Pseudo-Code For Console Menu

Image
In short, our menu-driven console application will use a While Loopthat will call a Main Menu method. The Main Menu method will print all menu options to the console and wait for the user to choose an option. Once the user selects an option, the program will execute the code block pertaining to the selected option. Here's …
See more on wellsb.com

The main() Method

  • In the previous two tutorials, we learned some useful C# string manipulation techniques - reverse string, and remove whitespace. In this tutorial, our menu-driven console application will use some of the methods we wrote in the previous tutorials and combine them into a useful, fully functional string manipulation program. We will begin with the static void Main()method. We want the appli…
See more on wellsb.com

A Complete Example

  • Now, we will simply add the [ReverseString()](https://wellsb.com/csharp/beginners/string-manipulation-reverse-string-csharp/) and [RemoveWhitespace()](https://wellsb.com/csharp/beginners/string-manipulation-csharp-trim-whitespace/) methods from our previous tutorials and display the results to the user. Here is ou…
See more on wellsb.com

The Bottom Line

  • In this tutorial, you learned how to add a menu to your C# console application. You created a method to display a menu and called that method from inside a whileloop. You processed the user's selection and triggered the relevant methods. Finally, you returned a boolean value to determine whether to continue to show the menu. This technique allows you to create more co…
See more on wellsb.com

A B C D E F G H I J K L M N O P Q R S T U V W X Y Z 1 2 3 4 5 6 7 8 9