Menu creation using switch-case – Computer Sir Ki Class

Login


Lost your password?

Don't have an account ?
Register (It's FREE) ×
  

Login
[lwa]



Code Learning #CPP#1978 siteicon   siteicon   siteicon  

Menu creation using switch-case

Creating a menu using switch case and run appropriate code piece based on menu selection.

Learning Objectives

  • Menu creation using switch case.
  • Use of cmath header for sqrt() function
  • Use of exit() function

Program Approach

  • Codes for different actions of area, perimeter and diagonal shall be written in different cases as given by the menu choices.

Source Code

TC++ #1978

Source Code

Run Output

Enter side of square =12
1. Find Area
2. Find Perimeter
3. Find Diagonal
4. Exit
Enter your choice : 1
Area = 144

-OR-
Enter side of square =12
1. Find Area
2. Find Perimeter
3. Find Diagonal
4. Exit
Enter your choice : 3
Diagonal = 16.9706

Code Understanding

cout << “1. Find Area ” <<endl;
cout << “2. Find Perimeter ” <<endl;
cout << “3. Find Diagonal ” <<endl;
cout << “4. Exit ” << endl;
cout << “Enter your choice : ” ;
cin >> c ;
This is a simple way to show a menu and collect the option number in an integer variable.

case 1: cout << “Area = ” << s*s ; break ;
case 2: cout << “Perimeter = ” << 4*s ; break ;
case 3: cout << “Diagonal = ” << s*sqrt(2); break ;
Different functional formulas are used as per the menu choice.

case 4:exit(0);
This is a case where exit(0) function is used. exit(0) is equivalent to returning to operating system directly with 0 code passed to it. 0 code usually represent the successful completion of program without any known error.

default: cout << “Wrong Menu Choice” ;
Here we come out with option of wrong menu choice when user presses any key not present in the menu.

Notes

  • These kind of programs are usually written in do-while loop, so that if wrong menu choice is there the menu can be shown again.

Common Errors

  • Formula as per menu choice should be individually tested. Such programs often suffer from incorrect placement of formulas for a particular menu choice.


Suggested Filename(s): menu-using-switch.cpp



Share

sunmitra| Created: 28-Dec-2017 | Updated: 15-Sep-2018|






×
Introductory Sessions Beginning to Program Tokens Keyword and Identifiers Data Types Variables and Constants Operators Simple User Input Building Expressions and Formulas Simple Real World Problems Simple If and If Else Multiple-Nested-Ladder of If Else Switch case selection Simple Loops Tricks in Loops - break continue scope Loop Applications - Handling numerals Series printing loops Nested Loops Pattern printing loops Number Varieties and Crunches String Handling (Null Terminated) Strings - string class type Functions (Built-in) Functions - user defined Functions Reference Passing/Returning Arrays Concepts and 1-D Arrays Array Data Management Two dimensional arrays and Matrices Structures Basics Structures passing/returning 2D Array Memory Addressing Display Using IO Manipulation Display Using C Formatting Tricks User Defined Data Types Enumerated Types Preprocessor Directives And Macros Exception Handling Programming Paradigms and OOPs Advantages Abstraction and Encapsulation Polymorphism Inheritance Function Overloading Concepts Function Overloading Varieties Function Overloading Special Cases Defining Classes Creating and Using Class Objects Class Members Accessibility Class Function Types Inline Functions Constant Functions Nesting of Functions Class Members Scope Resolution Static Members in a Class Array of Objects Constructor Concepts Default Constructor Parameterized Constructor Copy Constructor Constructor Overloading Destructors Inheritance Fundamentals Public Derivations Private and Protected Derivations Multiple Inheritance Multi-Level Inheritance Class Nesting Data File Concepts Handling Text Files Handling Binary Files Pointer Concepts Pointer and Arrays Pointers and Functions Object Pointers This Pointer Linked Lists Stacks Queues


Back