C++ | Introduction to Enumeration

Introduction

This is a tutorial for enumeration in C++. The program is given below that uses enumeration for discount system. The program is extendable. Go enjoy the program……

What is Enumeration?

Enumeration is set of constants. They have names.

Program for enumeration in C++.

#include<iostream.h>
#include<conio.h>
//declare enum
enum List
{
  boy = 15,
  girl =20,
  man = 5,
  woman = 10,
};
void main()
{
//clear the screen.
clrscr();
//declare int
int a,b;
//declare list
List discount;
//Input the data
cout<<"Enter the amount:";
cin>>a;
cout<<"Enter 1(boy), 2(girl), 3(man), (4)women :";
cin>>b;
//switch
switch(b)
{
case 1:
  discount = boy;
  break;
case 2:
  discount = girl;
  break;
case 3:
  discount = man;
  break;
case 4:
  discount = woman;
}
//calculate discount
a=a*discount/100;
//print discount
cout<<"Discount = "<<a;
//get character
getch();
}

Output
Enter the amount:100
Enter 1(boy), 2(girl), 3(man), (4)women :1
Discount = 15

How does it work.

  1. You enter amount.
  2. You enter gender.
  3. The discount is calculated and printed.

Extending it.

The program is extendable. This is a basic concept of c++ and can be used in any program. Go extend it..

Explanation.

  1. Include ‘iostream.h’ and ‘conio.h’ and ‘fstream.h’ files.
  2. Declare enum.
  3. Add void main.
  4. Start program by first clearing the screen.
  5. Declare variables.
  6. Declare your enum variable.
  7. Add cout and cin to get data.
  8. Add a switch statement.
  9. Calculate the discount.
  10. Print the discount.
  11. Get character.

At the end

You learnt creating the C++ program for using enumeration. So now enjoy the program.

Please  share it.

Sharing options: