Enumeration Error

Getting an error on the cin>> classes. On the operand sign >>. What i'm trying to do here is asking the user for his favorite subject, in which they will type history, math, geometry, pe, or science. Once they do that. under void display function, I will use that subject parameter and do the following.

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
/Enumeration for classes

enum subjects { history, math, geometry, pe, science};

// Void function display and get the favorite class from the user
void getfavoritesubject (subjects & classes)
{
	cout<<"What is your favorite subject";
	
	cin >> classes; //get an error on ">>" operand. What would be wrong here?

}

void choice (subjects  classes)
{
	if (classes == history)
		cout<<"Your In history";


}
int _tmain(int argc, _TCHAR* argv[])
{
	subjects replacesubject;

	getfavoritesubject(replacesubject);


	
	
	_getch();
	return 0;
}
you need to overload the >> operator so that it understands what to do with type subjects.
Last edited on
Wait, so how do I overload it? O_o
I changed my code quite a bit. My problem is how do I convert a string to enumeration type. If I can't then how do I save the string savegetfav under choosessubject? :(


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


//Enumeration for classes

enum courses { history, math, geometry, pe, science};

// Void function display and get the favorite class from the user
string getfavoritesubject ()
{
	cout<<"What is your favorite subject";
	string subject;
	cin >> subject; //get an error on ">>" operand. What would be wrong here?
	return subject;
}

void choice (courses  classeschosen)
{
	if (classeschosen == history)
		cout<<"Your In history";


}
int _tmain(int argc, _TCHAR* argv[])
{
	string savegetfav;

	savegetfav=getfavoritesubject();
	
	courses choosesubject;
	
	choosesubject=savegetfav;
	switch ( choosesubject)
	{
	case 'math':
		choosesubject=math;
	
	
	}
	
	_getch();
	return 0;
}
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
52
53
54
//Enumeration for classes
#include <iostream>
#include <string>
using namespace std;
enum courses { history, math, geometry, pe, science };


// Void function display and get the favorite class from the user
string getfavoritesubject()
{
	cout << "What is your favorite subject";
	string subject;
	cin >> subject; //get an error on ">>" operand. What would be wrong here?
	return subject;
}

void choice(courses  classeschosen)
{
	if (classeschosen == history)
		cout << "Your In history";


}

courses stringToCourses(string inString)
{
	if (inString == "history")
		return history;
	else if (inString == "math")
		return math;
	else if (inString == "geometry")
		return geometry;
	else if (inString == "pe")
		return pe;
	else if (inString == "science")
		return science;
}
int main()
{
	string savegetfav;

	savegetfav = getfavoritesubject();

	courses choosesubject;



	choosesubject = stringToCourses(savegetfav);



	cin.ignore(10000, '\n');
	return 0;
}
Topic archived. No new replies allowed.