ENUM and Structures Using Enumerated and structure data types, create a world th
ID: 3607300 • Letter: E
Question
ENUM and Structures Using Enumerated and structure data types, create a world that consists of 3 planets. There will be a structure named planet. This structure will contain distance from Earth, Atmosphere, language, and people. the variable Atmosphere is to be of structure data type Enviro. In this data type will exist the following members: percento2, percentnitro, humidity, temp. The variable language is to be of Language data type. In the language structure will exist the following members: primary, secondary, num_of_charact. The first 2 will be string variables. The people variable in the structure will be of People or Inhabitant data type. In this Inhabitant structure will exist population, avgheight, and an enumerated variable to describe the general temperament,which will be of datatype mood described below:Explanation / Answer
#include <iostream>
using namespace std;
enum Enviro {
percento2=0,
percentnitro=1,
humidity=2,
temp=3
};
string EnviroString[] =
{
"percento2",
"percentnitro",
"humidity",
"temp"
};
// When we scan enums, we have to scan integers depicting the index of value in enum
// inorder to print the string value of the enum, we have to store them with same index in array of strings
// Suppose user enters 1, we have to do MoodString[1] which will gve GENEROUS
string MoodString[] = {
"FRIENDLY",
"GENEROUS",
"HOSTILE",
"OPPORTUNISTIC"
};
enum Mood {
FRIENDLY,
GENEROUS,
HOSTILE,
OPPORTUNISTIC
};
struct Language {
string primary;
string secondary;
int num_of_charact;
};
struct Inhabitant {
long population;
double avgheight;
Mood mood;
};
struct Planet {
double distFromEarth;
Enviro atomosphere;
Language language;
Inhabitant* people;
};
int main() {
// your code goes here
Planet mercury;
cout<<"Please enter the distFromEarth"<<endl;
cin>>mercury.distFromEarth;
// As the atmosphere is of Enviro type and we have scanned an integer
// we need to convert the integer to Enviro type by statically casting int to Enviro
cout<<"Please enter the atomosphere"<<endl;
int userOption;
cin>>userOption;
mercury.atomosphere = static_cast<Enviro>(userOption);
cout<<"Please enter the primary language"<<endl;
cin>>mercury.language.primary;
cout<<"Please enter the secondary language"<<endl;
cin>>mercury.language.secondary;
cout<<"Please enter the number of characters"<<endl;
cin>>mercury.language.num_of_charact;
cout<<"Please enter the number of types of inhibitants in planet"<<endl;
int numOfInhibitants;
cin>>numOfInhibitants;
// As this is an array inside the struct, its size is not pre-allocated
// allocate the size as per user input
Inhabitant * arr = new Inhabitant[numOfInhibitants];
mercury.people = arr;
for(int i=0;i<numOfInhibitants;i++) {
cout<<"Please enter the population"<<endl;
cin>>mercury.people[i].population;
cout<<"Please enter the average height"<<endl;
cin>>mercury.people[i].avgheight;
cout<<"Please enter the mood"<<endl;
cin>>userOption;
// As the atmosphere is of Mood type and we have scanned an integer
// we need to convert the integer to Mood type by statically casting int to Mood
mercury.people[i].mood = static_cast<Mood>(userOption);
}
cout<<"Distance from earth is "<<mercury.distFromEarth<<endl;
cout<<"Environment is "<<EnviroString[mercury.atomosphere]<<endl;
cout<<"Primary Language is "<<mercury.language.primary<<endl;
cout<<"Secondary Language is "<<mercury.language.secondary<<endl;
cout<<"Language characters are "<< mercury.language.num_of_charact<<endl;
for(int i=0;i<numOfInhibitants;i++) {
cout<<"The inhabitant properties are "<<mercury.people[i].population<<" " <<mercury.people[i].avgheight<<" "<<MoodString[mercury.people[i].mood]<<endl;
}
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.