C++ | Creating a menu driven program

Introduction

This post will create a menu driven program to calculate the area and perimeter of shapes. The first program calculates the area of given shapes. The next program calculates perimeter of given shapes. The program is expandable by adding more geometrical shapes in the program. Go enjoy the program. Lets begin……………….

Program for area of shapes.

#include<iostream.h>
#include<conio.h>
void main()
{
//clear the screen.
clrscr();
//declare variable type float and char
float a,b,area;
char ch;
//Input the choice.
cout<<"Enter c for circle"<<endl;
cout<<"Enter s for square"<<endl;
cout<<"Enter r for rectangle"<<endl;
cout<<"Enter t for triangle"<<endl;
cin>>ch;
//conditional switch statement.
switch (ch)
{
case 'c':
cout<<"Enter radius"<<endl;
cin>>a;
area=3.14*a*a;
break;
case 's':
cout<<"Enter the side"<<endl;
cin>>a;
area=a*a;
break;
case 'r':
cout<<"Enter the length"<<endl;
cin>>a;
cout<<"Enter the breadth"<<endl;
cin>>b;
area=a*b;
break;
case 't':
cout<<"Enter the height"<<endl;
cin>>a;
cout<<"Enter the base"<<endl;
cin>>b;
area=0.5*a*b;
break;
default:
cout<<"Syntax Error";
}
//print the area.
cout<<"Area is "<<area;
//get character
getch();
}

Output

Enter c for circle
Enter s for square
Enter r for rectangle
Enter t for triangle
t
Enter the height
12
Enter the base
6
Area is 36.

Program for perimeter of shapes.

#include<iostream.h>
#include<conio.h>
void main()
{
//clear the screen.
clrscr();
//declare variable type float and char
float a,b,peri;
char ch;
//Input the choice.
cout<<"Enter c for circle"<<endl;
cout<<"Enter s for square"<<endl;
cout<<"Enter r for rectangle"<<endl;
cin>>ch;
//conditional switch statement.
switch (ch)
{
case 'c':
cout<<"Enter radius"<<endl;
cin>>a;
peri=2*3.14*a;
break;
case 's':
cout<<"Enter the side"<<endl;
cin>>a;
peri=4*a;
break;
case 'r':
cout<<"Enter the length"<<endl;
cin>>a;
cout<<"Enter the breadth"<<endl;
cin>>b;
peri=2*(l+b);
break;
default:
cout<<"Syntax Error";
}
//print the area.
cout<<"Perimeter is "<<peri;
//get character
getch();
}

Output

Enter c for circle
Enter s for square
Enter r for rectangle
s
Enter the side
12
Area is 48

How does it work

  1. You enter the choice.
  2. The conditional statement decides shape’s formula.
  3. You enter the necessary inputs.
  4. Area or Perimeter in calculated.
  5. The area or perimeter is printed.

Extending it

The program can be extended by using more shapes in the program. For example, you can add trapezium, parallelogram, or more shapes. Go ahead and extend it…..

Explanation.

  1. Include ‘iostream.h’ and ‘conio.h’ files.
  2. Add void main.
  3. Start program by first clearing the screen.
  4. Declare the variables as float and char (name them as you want.)
  5. Decide the shape by using switch statement.
  6. Add cout and cin of variable of input of necessary inputs.(as shown in program)
  7. Calculate the area or perimeter.
  8. Print area or perimeter.

At the end

You learnt creating the c++ program of creating a menu driven program. So now enjoy the program.

Please comment on the post and share it.
And like it if you liked.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s