473,326 Members | 2,148 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Input to enum

I have:

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

How do I input information like this:

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

Apr 11 '06 #1
6 6637
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
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
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
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
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
Thank you all for your help!

Apr 11 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: namo | last post by:
Hello, I have started a small simulation project to learn C++ and I am faced with the following problem : the user can choose at runtime to simulate protocol P1 or P2. P1 uses packets of type...
34
by: Steven Nagy | last post by:
So I was needing some extra power from my enums and implemented the typesafe enum pattern. And it got me to thinking... why should I EVER use standard enums? There's now a nice little code...
35
by: dtschoepe | last post by:
Greetings. I am working on an assignment and can't seem to get the right concept for something I'm attempting to do with enum data types. I have defined the following in my code: enum color...
77
by: arnuld | last post by:
1st I think of creating an array of pointers of size 100 as this is the maximum input I intend to take. I can create a fixed size array but in the end I want my array to expand at run-time to fit...
209
by: arnuld | last post by:
I searched the c.l.c archives provided by Google as Google Groups with "word input" as the key words and did not come up with anything good. C++ has std::string for taking a word as input from...
21
by: arnuld | last post by:
I have created a program to print the input words on stdout. Input is taken dynamically from stdin. In each word, each input character is allocated dynamically. I have ran this program with a file...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.