Public Group

# enum's

This topic is 3517 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

im working through a book (Beginning C++ Game Programming) and one of the examples involves this code (only part). int _tmain(int argc, _TCHAR* argv[]) { enum fields {WORD, HINT, NUM_FIELDS}; const int NUM_WORDS =5; const string WORDs[NUM_WORDS][NUM_FIELDS]; { {"wall","Do you feel you're banging your head against something?"}, {"glasses", "These might help you see the answer. ";}, {"labored","Going slowly, is it? ";}, {"persistent","Keep at it. "}, {"jumble", "Its what the game is all about. "}, }; yet there are compiling proplems , i personaly dont fully understand enum's and cant find a decent tutorial. so can anyone fix this and recommend a tutorial, thatd be great. Cheers Blair

##### Share on other sites
I can see the problem. However, I will refrain from pointing it out until you post the exact error message.

##### Share on other sites
Quote:
 yet there are compiling proplems

What problems are those? If you tell us the error, we can help.

Also, I see nothing wrong with the enum, but I do see a different mistake you are making.

Oh, and the Google brings up 744,000 results for "C++ enums", but the second hit ought to help you. (CProgramming 'Enums')

Enums work alot like constants, except also have their own typedef'd int-like variable that can't be set to anything accept the enums.

##### Share on other sites
Quote:
 Original post by Catkillim working through a book (Beginning C++ Game Programming) and one of the examples involves this code (only part).int _tmain(int argc, _TCHAR* argv[]){ enum fields {WORD, HINT, NUM_FIELDS}; const int NUM_WORDS =5; const string WORDs[NUM_WORDS][NUM_FIELDS]; { {"wall","Do you feel you're banging your head against something?"}, {"glasses", "These might help you see the answer. ";}, {"labored","Going slowly, is it? ";}, {"persistent","Keep at it. "}, {"jumble", "Its what the game is all about. "}, // <<-- The comma shouldn't be there };yet there are compiling proplems , i personaly dont fully understand enum's and cant find a decent tutorial. so can anyone fix this and recommend a tutorial, thatd be great.CheersBlair

Your use of enums is correct(The added comment on the "jumble" line), it's just that your array definition is incorrect.

However, if you encounter problems like this and post on a board like gamedev, please also post the error message. It's extremely difficult to re-produce a compiler error without complete code and without the exact error message.

As said, post the error message you got, and we can see if I'm correct.

Toolmaker

##### Share on other sites
Quote:
 Original post by ToolmakerYour use of enums is correct(The added comment on the "jumble" line), it's just that your array definition is incorrect.

He also needs a equal sign, actually assigning the data to the array. [wink]

##### Share on other sites
error message, sorry people:

1>------ Build started: Project: Word Jumble, Configuration: Debug Win32 ------
1>Compiling...
1>Word Jumble.cpp
1>c:\users\blair\documents\visual studio 2008\projects\word jumble\word jumble\word jumble.cpp(14) : error C2143: syntax error : missing ';' before '}'
1>c:\users\blair\documents\visual studio 2008\projects\word jumble\word jumble\word jumble.cpp(14) : error C2143: syntax error : missing ';' before ','
1>c:\users\blair\documents\visual studio 2008\projects\word jumble\word jumble\word jumble.cpp(15) : error C2143: syntax error : missing ';' before '{'
1>c:\users\blair\documents\visual studio 2008\projects\word jumble\word jumble\word jumble.cpp(15) : error C2143: syntax error : missing ';' before ','
1>c:\users\blair\documents\visual studio 2008\projects\word jumble\word jumble\word jumble.cpp(16) : error C2143: syntax error : missing ';' before '{'
1>c:\users\blair\documents\visual studio 2008\projects\word jumble\word jumble\word jumble.cpp(16) : error C2143: syntax error : missing ';' before ','
1>c:\users\blair\documents\visual studio 2008\projects\word jumble\word jumble\word jumble.cpp(17) : error C2143: syntax error : missing ';' before '{'
1>c:\users\blair\documents\visual studio 2008\projects\word jumble\word jumble\word jumble.cpp(17) : error C2143: syntax error : missing ';' before '}'
1>c:\users\blair\documents\visual studio 2008\projects\word jumble\word jumble\word jumble.cpp(17) : error C2143: syntax error : missing ';' before ','
1>c:\users\blair\documents\visual studio 2008\projects\word jumble\word jumble\word jumble.cpp(18) : error C2143: syntax error : missing ';' before '{'
1>c:\users\blair\documents\visual studio 2008\projects\word jumble\word jumble\word jumble.cpp(18) : error C2143: syntax error : missing ';' before '}'
1>Build log was saved at "file://c:\Users\Blair\Documents\Visual Studio 2008\Projects\Word Jumble\Word Jumble\Debug\BuildLog.htm"
1>Word Jumble - 11 error(s), 0 warning(s)

##### Share on other sites
Well, I feel for you.

I'm going to pretend to be the compiler for you, so you can see why it can't communicate to you very effectively. And maybe it will help you see the real mistake, if you think about it carefully. (Developing this kind of discipline is important; programming requires learning from your mistakes and thinking for yourself.)

int _tmain(int argc, _TCHAR* argv[]){

"Okay, here's the main function."

enum fields {WORD, HINT, NUM_FIELDS};

"Okay, 'fields' is an enumeration of 3 possible values: WORD (which corresponds to integer value 0), HINT (which corresponds to integer value 1) and NUM_FIELDS (which corresponds to integer value 2)."

const int NUM_WORDS =5;

"Okay, 'NUM_WORDS' is a constant integer with the value of 5."

const string WORDs[NUM_WORDS][NUM_FIELDS];

"Okay, 'WORDs' is a 2-dimensional array of strings. The first dimension has a size of 'NUM_WORDS' - okay, this is easy, I know that one, that's 5. So there are 5 rows, and each one has NUM_FIELDS strings in it... er, that's an enumeration value, but that's okay; enumeration values can be used implicitly as integers, and NUM_FIELDS corresponds to 2. So I have a 5 by 2 grid of strings named WORDs. And um... okay, I guess there's nothing in it to start. 'string' (actually std::basic_string<char>) is a non-POD class with a default constructor, so I'll fill the array with default-constructed ('blank') strings to start off with."

{

"Okay, we're starting an anonymous scope."

{

"Okay, another one..."

"wall","Do you feel you're banging your head against something?"

"Okay... so evaluate the string literal "wall", throw away the result of that expression (which of course is just "wall"), and then evaluate the other string literal, and then..."

},

"WAIT! What? I was in the middle of that statement, and suddenly here's a closing brace for the anonymous scope! I need a semicolon to terminate that statement first! Okay, let's skip past that for a second and try to make sense of the rest of the code... WAIT! What's this other comma doing here? That looks like another comma operator, but there's nothing on the left side to evaluate!"

And of course, the compiler just gets more and more confused from there on.

It's not too late to change languages, you know. (Do you feel you're banging your head against something?)

1. 1
2. 2
frob
16
3. 3
4. 4
Rutin
11
5. 5

• 13
• 13
• 60
• 14
• 15
• ### Forum Statistics

• Total Topics
632124
• Total Posts
3004248

×