Speed up development with full-stack environments for every branch.

Learn More

C++ Examples for Enumerated types

213 Runs 1708 Views 365 Copies
Saved

Saved

tianxiasoft 13

tianxiasoft
published 4 years ago

#include <iostream>

using namespace std;

/******************************************************************************************/
/*  This example shows an enumerated types.  							                  */
/******************************************************************************************/

// main function
int main()
{
	enum GENDER { MALE = 0, FEMALE = 1, GENDER_COUNT};
	enum JOB	{ STUDENT = 0, PROFESSOR, ENGINEER, WORKER, JOB_COUNT };
	int genders[5];
	int	jobs[5];
	int i = 0;
	// input gender and jobs of 5 people.
	for (; i < 5; i ++)
	{
		cout << "No." << i + 1 << endl;
		cout << "Input Gender(0=Male, 1=Female) : ";
		cin >> genders[i];
		cout << "Job(0=student, 1=professor, 2=engineer, 3=worker) ";
		cin >> jobs[i]; 
	}

	int male_count = 0;
	int female_count = 0;
	int student_count = 0;
	int professor_count = 0;
	int engineer_count = 0;
	int worker_count = 0;

	for (i = 0; i < 5; i ++)
	{
		// count number of males and females
		if (genders[i] == MALE)
			male_count ++;
		else if (genders[i] == FEMALE)
			female_count ++;

		// count number of each jobs
		switch (jobs[i])
		{
		case STUDENT:
			student_count ++;
			break;
		case PROFESSOR:
			professor_count ++;
			break;
		case ENGINEER:
			engineer_count ++;
			break;
		case WORKER:
			worker_count ++;
			break;
		default:
			break;
		}
	}

	// print the result
	cout << "Number of male " << male_count << endl;
	cout << "Number of female " << female_count << endl;
	cout << "Number of student " << student_count << endl;
	cout << "Number of professor " << professor_count << endl;
	cout << "Number of engineer " << engineer_count << endl;
	cout << "Number of worker " << worker_count << endl;

	// wait for any key pressing
	cin.get();
	return 0;
}
Please login/signup to get access to the terminal.

Your session has timed out.

Dismiss (the page may not function properly).