char to enum type?

How could I convert a char from cin to a specified enum type?

The enum type is as follows in entry.h:
 
enum Category {SONG, POEM, ESSAY, NOVEL, NOTE};



and what I have in my other function needs to convert the char from cin to the corresponding Category in entrylist.cpp.

Would something like this work?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

void Entrylist::Add(char newTitle[], char newAuthor[], char newStyle, int newSize)
{

Category newCat;

switch (newStyle)
  switch (newStyle)
    {
        case 'S':
                 break;
        case 'P':
                 break;
        case 'E':
                 break;
        case 'N':
                 break;
        case 'T':
                 break;
            
    }


would static cast work?
Last edited on
that switch would work. providing you remove one of the duplicate lines of course (7 and 8).

you wouldnt need a static cast.

Would something like this work?

Just give it a go :)


e.g.
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
#include<iostream>

using namespace std;

enum Category { SONG, POEM, ESSAY, NOVEL, NOTE, UNDEFINED };


Category convert(char c)
{
	Category rtn = UNDEFINED;

	switch (c)
	{
	case 's':
		rtn = SONG;
		break;
	case 'p':
		rtn = POEM;
	default:
		rtn = UNDEFINED;
		break;
	}
	
	return rtn;

}

int main()
{

	Category newCat = convert('s');
	
	
	// newCat is now SONG
	
	return 0;

}
Last edited on
Topic archived. No new replies allowed.