by Dinesh Thakur Category: Control Structures

An enumerated data type is also an user defined data type. The enum keyword is given before the enumerated data type. A list can tell the compiler which values a given variable can take. Consider the following example:

enum temp{Veryhot, Hot, Medium, Cold};

temp is of enum type and has four members Veryhot, Hot, Medium and Cold. The value 0 is assigned to Veryhot, value 1 is assigned to Hot, 2 is assigned to Medium and so on. The enum specifier allows the user to specify the possible value of the type. If an initial value of -1 is assigned to the first element of the enum type, the subsequent elements are assigned the values 0, 1, 2, etc.

A variable of enum type can be declared as follows:

temp t1;

This indicates that t1 is a variable of type temp which is of enum type.

Example

Write C++ program illustrates the use of enumerated data type.

#include <iostream.h>

#include<conio.h>
enum qlty{excellent, good, average};
struct product
 {
    char prodno[10];
    char prodname[20];
    qlty q1;
 } prod;
void main()
{
   clrscr();
   cout<<"Enter Product Number :";
   cin>>prod.prodno;
   cout<<"Enter Product Name :";
   cin>>prod.prodname;
   cout<<"Enter quality code for the product.";
   cout<<"\n0 for excellent, 1 for good, 2 for average \n";
   cin>>int(prod.q1);
   cout<<"\n\nProduct Number :"<<prod.prodno;
   cout<<"\nProduct Name :"<<prod.prodname;
   if(prod.q1==0)
      cout<<"\nQuality Excellent";
   else if(prod.q1==1)
      cout<<"\nQuality Good ";
   else
      cout<<"\nQuality Average ";
      getch();
}

enumerated data type



About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.



Related Articles