Enumerating the day of week using the enum data type C++ code sample

 

Compiler: Visual C++ Express Edition 2005

Compiled on Platform: Windows 2003 Server Standard Edition

Header file: Standard

Additional project setting: Set project to be compiled as C++

Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C++ Code (/TP)

Other info: none

To do: Displaying the days from enum data type

To show: How to define, declare and use the enum data type in C++ program

 

#include <iostream>

using namespace std;

 

// Define the  enum data type Days

enum Days

{

monday, //monday = 0 by default

tuesday = 0,//tuesday = 0 also

wednesday, //wednesday = 1

thursday, //thursday = 2

friday, //an so on.

saturday,

sunday

};

 

int main(void)

{

// Declaring the enum variable

// Try changing the tuesday constant, recompile and re run this program

enum Days WhatDay = tuesday;

 

switch (WhatDay)

{

case 0: cout<<"It's Monday"<<endl;

break;

default: cout<<"Other day"<<endl;

}

return 0;

}

 

Output example:

 

It's Monday

Press any key to continue . . .

 

 

C and C++ Programming Resources | C & C++ Code Example Index