Techtopz is a blog on programming and tech stuff.
Techtopz is now on UpHub.
Follow us for more updates.


C++ | Introduction to Enumeration

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++.

//declare enum
enum List
  boy = 15,
  girl =20,
  man = 5,
  woman = 10,
void main()
//clear the screen.
//declare int
int a,b;
//declare list
List discount;
//Input the data
cout<<"Enter the amount:";
cout<<"Enter 1(boy), 2(girl), 3(man), (4)women :";
case 1:
  discount = boy;
case 2:
  discount = girl;
case 3:
  discount = man;
case 4:
  discount = woman;
//calculate discount
//print discount
cout<<"Discount = "<<a;
//get character

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..

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.

Login to like the post