By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,720 Members | 2,185 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,720 IT Pros & Developers. It's quick & easy.

Input to enum

P: n/a
I have:

struct person
{
enum gender {male, female};
};

How do I input information like this:

person John;
....
cin >> John.gender?
....

Apr 11 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a
da****************@gmail.com wrote:
I have:

struct person
{
enum gender {male, female};
You declared a type, you declared its possible values. But you didn't
declare any data member that would carry the value. Did you mean to do

gender sex;

here?
};

How do I input information like this:

person John;
...
cin >> John.gender?
...


The best "work-around" would be to enter a char and then if it is 'm'
or 'M', set 'John.sex' to 'person::male', and if the char is 'f' or
'F', set 'John.sex' to 'person::female', and otherwise report bad input.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Apr 11 '06 #2

P: n/a
enum gender {male, female}; is in a header file and it seems that it
doesn't want to accept it in the main file.

when I do in .CPP in main function:

gender sex;

and then sex. <-- it's not showing me male or female

Apr 11 '06 #3

P: n/a
da****************@gmail.com wrote:
enum gender {male, female}; is in a header file and it seems that it
doesn't want to accept it in the main file.

when I do in .CPP in main function:

gender sex;

and then sex. <-- it's not showing me male or female


It won't. The enumerators ('male' and 'female') in your 'gender' type
are not members.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Apr 11 '06 #4

P: n/a
da****************@gmail.com posted:
I have:

struct person
{
enum gender {male, female};
};

How do I input information like this:

person John;
...
cin >> John.gender?
...


class Person
{
public:
enum Gender {male, female} gender;
};

#include <iostream>
using std::cout; using std::cin; using std::endl;

int main()
{
Person marcus;

cout << "Specify Gender (M/F): ";

std::string answer;

cin >> answer;
//I never work with std::string, so I don't know
//a particularly efficient way of getting the first
//character...

switch ( *( answer.c_str() ) )
{
case 'm':
case 'M':
marcus.gender = male;
break;

case 'f':
case 'F':
marcus.gender = female;
break;
}
}

Apr 11 '06 #5

P: n/a
Tomás wrote:
[...]
switch ( *( answer.c_str() ) )
{
case 'm':
case 'M':
marcus.gender = male;
marcus.gender = Person::male;
break;

case 'f':
case 'F':
marcus.gender = female;
marcus.gender = Person::female;
break;
}
}


V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Apr 11 '06 #6

P: n/a
Thank you all for your help!

Apr 11 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.