Use of enum in array of structures
May 9, 2019 at 4:32pm UTC
You are required to create an array of struct type students. In which you are required to store the
gender of the student using the ENUM. Also, you need to use pointers to store student
information in the heap. Student should have following information
First Name
Last Name
Roll No
Gender
I am not getting how to use enum in this.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
#include <iostream>
#include <string>
using namespace std;
struct students
{
string name;
string lastname;
int rollno;
enum Gender { male, female } gender;
};
int main()
{
char c;
students *ptr = new students[4];
for (int i = 0; i < 1; i++)
{
cout << "Enter name:" << endl;
cin >> ptr[i].name;
cout << "Enter Last name:" << endl;
cin >> ptr[i].lastname;
cout << "Enter rollno:" << endl;
cin >> ptr[i].rollno;
cout << "Male or female: m/f" << endl;
cin >> c;
cout << endl;
if (c == 'm' )
{
ptr[i].gender = students::Gender::male;
}
else
{
ptr[i].gender = students::Gender::female;
}
}
for (int i = 0; i <1; i++)
{
cout << "Name:" << ptr[i].name;
cout << "Last name:" << ptr[i].lastname;
cout << "rollno:" << ptr[i].rollno;
if (c == 'm' )
{
cout << ptr[i].gender;
}
else
{
cout << ptr[i].gender ;
}
}
system("pause" );
}
May 9, 2019 at 4:45pm UTC
What specifically is the part you don't understand?
Lines 43 and 47?
At its core, an enum is just a nicer way to name integers.
When you declare enum Gender { male, female }, male will have the value 0, and female will have the value 1.
So when you print ptr[i].gender, it will either print a 0 or a 1, which probably isn't what you want.
If you want it to print a string, you have to do the conversion yourself.
e.g.
1 2 3 4 5 6 7 8
if (ptr[i].gender == students::Gender::male)
{
cout << "male" ;
}
else
{
cout << "female" ;
}
May 9, 2019 at 7:52pm UTC
I did some changings. Now the code looks more simple! But still its displaying Gender as 0. I want it to display either male or female.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
#include <iostream>
#include <string>
using namespace std;
enum Gender { male, female };
struct students
{
string name;
string lastname;
int rollno;
enum Gender { male =0, female=1 } gender;
};
int main()
{
char c;
students *ptr = new students[4];
for (int i = 0; i < 1; i++)
{
cout << "Enter name:" << endl;
cin >> ptr[i].name;
cout << "Enter Last name:" << endl;
cin >> ptr[i].lastname;
cout << "Enter rollno:" << endl;
cin >> ptr[i].rollno;
cout << "Male or female: m/f" << endl;
cin >> c;
cout << endl;
}
for (int i = 0; i <1; i++)
{
cout << "Name:" << ptr[i].name;
cout << "Last name:" << ptr[i].lastname;
cout << "rollno:" << ptr[i].rollno;
if (c == 'm' )
{
Gender g;
g = male;
cout << "Gender: " << g << endl;
}
else
{
Gender g;
g = female;
cout << "Gender: " << g << endl;
}
}
system("pause" );
}
May 9, 2019 at 11:08pm UTC
Yes, like I said before, you have to do the conversion to a string yourself.
Don't just cout << g.
1 2 3 4 5 6 7 8
if (g == male)
{
cout << "male" ;
}
else
{
cout << "female" ;
}
By the way, now that you also declared Gender in the global scope, you can change line 10 to just
Gender gender;
Edit: Also, two more things.
(1) you declare an array of 4 students, but you only ever loop through one of them.
To fix this, change your for loop to for (int i = 0; i < 4; i++)
(2)
You never assign ptr[i].gender. Don't rely on char c if you intend to have multiple students.
1 2 3 4 5
cin >> c;
if (c == 'm' )
ptr[i].gender = male;
else
ptr[i].gender = female;
...
1 2 3 4 5 6 7 8 9
cout << "gender: " ;
if (ptr[i].gender == male)
{
cout << "male" << endl;
}
else
{
cout << "female" << endl;
}
Last edited on May 9, 2019 at 11:14pm UTC
Topic archived. No new replies allowed.