Switch (enum) ... ?

Can you switch through enumerated data...here is what I tried that didn't work:

1
2
3
4
5
6
7
8
9
enum TEAMS {buffalo, miami, etc...};

TEAMS t;

switch(t) {

...

}

I could use arrays and number the team values, but this makes descriptions easier...

any ideas...or locations of information?
Last edited on
Yes, you can. What's the error?
1>.\NFL Analysis.cpp(9) : error C2059: syntax error : ')'
1>.\NFL Analysis.cpp(9) : error C2143: syntax error : missing ';' before '{'
1>.\NFL Analysis.cpp(10) : error C2046: illegal case
1>.\NFL Analysis.cpp(31) : error C2043: illegal break
1>.\NFL Analysis.cpp(33) : error C2046: illegal case
1>.\NFL Analysis.cpp(54) : error C2043: illegal break
1>.\NFL Analysis.cpp(57) : error C2047: illegal default
1>.\NFL Analysis.cpp(59) : error C2043: illegal break

Here is code:

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
55
56
57
58
59
60
enum TEAMS {BUF, MIA, NE, NYJ, BAL, CIN, CLE, PIT, HOU, IND, JAC, TEN, DEN, KC, OAK, SD, DAL, PHI, NYG, WSH, CHI, DET, GB, MIN, ATL, CAR, NO, TB, ARI, SF, SEA, STL, enumTypeEnd}; 
	
	string line;
	
	char *cstr, *y, *oppY;
	int count=0; 
	double yds=0, oppYds=0;
	for(int i=0; i<enumTypeEnd; i++) {    TEAMS eCurrent = (TEAMS) i;  cout << eCurrent << endl;   } 
	switch(TEAMS) {
		case BUF:
			{
			ifstream buffalo ("c:/buf.txt");
			while(buffalo.good()) {
				getline(buffalo, line);
				count++;
				if(count==187) {
					cstr = new char [line.size()+1];
					strcpy (cstr, line.c_str());
					y=strtok (cstr,"					<td style=\"text-align:right;\">");
					yds = atoi(y);
					cout << yds << endl;
				}
				if(count==189) {
					cstr = new char [line.size()+1];
					strcpy (cstr, line.c_str());
					oppY=strtok (cstr,"					<td style=\"text-align:right;\">");
					oppYds = atoi(oppY);
					cout << oppYds << endl;
				}
			}
			break;
		}
		case 1:
		{
			ifstream miami ("c:/mia.txt");
			while(miami.good()) {
				getline(miami, line);
				count++;
				if(count==187) {
					cstr = new char [line.size()+1];
					strcpy (cstr, line.c_str());
					y=strtok (cstr,"					<td style=\"text-align:right;\">");
					yds = atoi(y);
					cout << yds << endl;
				}
				if(count==189) {
					cstr = new char [line.size()+1];
					strcpy (cstr, line.c_str());
					oppY=strtok (cstr,"					<td style=\"text-align:right;\">");
					oppYds = atoi(oppY);
					cout << oppYds << endl;
				}
			}
			break;
		}
			
		default:
			assert( ! "Invalid TEAMS enum value" );
			break;
	}
Last edited on
switch(TEAMS)
This isn't like what you posted earlier.
1
2
3
TEAMS t;

switch(t)

TEAMS is a type. switch works on expressions, and a type name isn't an expression.

Also,
1
2
3
case BUF:
//...
case 1:
Alright. Pick one.
ok, getting tired or something...here is code, adjusted but nothing prints out when case BUF: or case 0:, or case MIA: or case 1:

I get the warning: uninitialized local variable 't' used

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
55
56
57
enum TEAMS {BUF, MIA, NE, NYJ, BAL, CIN, CLE, PIT, HOU, IND, JAC, TEN, DEN, KC, OAK, SD, DAL, PHI, NYG, WSH, CHI, DET, GB, MIN, ATL, CAR, NO, TB, ARI, SF, SEA, STL, enumTypeEnd}; 
	TEAMS t;
	string line;
	
	char *cstr, *y, *oppY;
	int count=0; 
	double yds=0, oppYds=0;
	//for(int i=0; i<enumTypeEnd; i++) {    TEAMS eCurrent = (TEAMS) i;  cout << eCurrent << endl;   } 
	switch(t) {
		case 0:
		{
			ifstream buffalo ("c:/buf.txt");
			while(buffalo.good()) {
				getline(buffalo, line);
				count++;
				if(count==187) {
					cstr = new char [line.size()+1];
					strcpy (cstr, line.c_str());
					y=strtok (cstr,"					<td style=\"text-align:right;\">");
					yds = atoi(y);
					cout << yds << endl;
				}
				if(count==189) {
					cstr = new char [line.size()+1];
					strcpy (cstr, line.c_str());
					oppY=strtok (cstr,"					<td style=\"text-align:right;\">");
					oppYds = atoi(oppY);
					cout << oppYds << endl;
				}
			}
                                count = 0
			break;
		}
		case 1:
		{
			ifstream miami ("c:/mia.txt");
			while(miami.good()) {
				getline(miami, line);
				count++;
				if(count==187) {
					cstr = new char [line.size()+1];
					strcpy (cstr, line.c_str());
					y=strtok (cstr,"					<td style=\"text-align:right;\">");
					yds = atoi(y);
					cout << yds << endl;
				}
				if(count==189) {
					cstr = new char [line.size()+1];
					strcpy (cstr, line.c_str());
					oppY=strtok (cstr,"					<td style=\"text-align:right;\">");
					oppYds = atoi(oppY);
					cout << oppYds << endl;
				}
			}
			break;
		}
	}
Last edited on
Well, t isn't initialized to anything.
is there any reason that nothing prints?
t contains garbage which is different from any case contemplated in the switch statement, and nothing is printed outside of the switch statement. Thus, nothing is printed inside or outside the switch statement. Thus, nothing at all is printed.
i'm referring to the cout statements...i rewrote the code so that TEAMS t; is included...isn't that the "switchable expression" you were referring to earlier?
Your switch executing either case depends on t having a value that's equivalent to 0 or 1, but t hasn't been assigned any value at all, which means it contains garbage and almost certainly neither 0 nor 1, which means that neither case of the switch will get executed.
1
2
3
4
5
6
7
8
9
10
11
12
TEAMS t; //No assignment. t contains garbage.

//t is untouched.

switch (t){
    case FOO:
        std::cout <<"Foo.\n";
        break;
    case BAR:
        std::cout <<"Bar.\n";
}
//What's the output? 

I don't know how to say it any clearer
my question was how to switch through an enum...I can write 32 if statements...or put the teams in an array...but I want to know how I can switch through an enum...if its possible, which you said it was...
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
enum Color{
    RED,
    GREEN,
    BLUE,
    CYAN,
    MAGENTA,
    YELLOW,
    WHITE,
    BLACK
};

Color c=/*...*/;
switch (c){
    case RED:   //...
    case GREEN: //...
    case BLUE:  //...
    //...
}
Last edited on
Topic archived. No new replies allowed.