Technical Question

Hey all,

Hope all is going great!

I'm experimenting with a jumble game from a book (Beginning C++ Game Programming) I'm reading and I need alittle help with an explaination of how a particular part of the code is working. I'm going to break it down the best I can as how I think it's working and I'd appreciate it if someone could steer me in the right direction please.

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

 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. "}
};


srand(time(0));
int choice = (rand() % NUM_WORDS);
string theWord = WORDS[choice][WORD]; // word to guess
string theHint = WORDS[choice][HINT]; // hint for word


The part of that code I need the help on is this part:

1
2
3
enum fields {WORD, HINT, NUM_FIELDS};
   const int NUM_WORDS = 5;
   const string WORDS[NUM_WORDS][NUM_FIELDS]


I just included the rest to help provide you with additional info so you got a better idea of what's going on. Now, from what I know, enumerators start at 0 and increment with each element. So enum fields is indexed as so: {0, 1, 2}. The NUM_WORDS is initialized to 5 which in turn is the number used to determine how many "storage" elements are created in the WORDS array under NUM_WORDS and NUM_FIELDS (which is index 2), creates that many storage elements within the NUM_FIELDS section of WORDS array. Right? Or am I way off base here?

Now, how does the program "know" to store the WORD and HINT elements in the NUM_FIELDS section of the array and store those with the actual words and hints used in the game and not place them within the NUM_WORDS section or does it? For example, is this how it looks if drawn out on paper?

1
2
3
Array  Storage Element  Storage Element  
WORDS     NUM_WORDS        NUM_FIELDS
       [0][1][2][3][4]    [WORD][HINT]


Sorry if this is simple, but I'm having difficulty wrapping my head around the process here.

Thanks for all the help.
Last edited on

The NUM_WORDS is initialized to 5 which in turn is the number used to determine how many "storage" elements are created in the WORDS array under NUM_WORDS and NUM_FIELDS (which is index 2), creates that many storage elements within the NUM_FIELDS section of WORDS array. Right? Or am I way off base here?


What this piece of code does is that it creates a constant multidimensional array named as WORDS which has dimensions 5 and 2.


Now, how does the program "know" to store the WORD and HINT elements in the NUM_FIELDS section of the array and store those with the actual words and hints used in the game and not place them within the NUM_WORDS section or does it


The words are stored in a string array WORDS not in NUM_WORDS.

1
2
3
4
5
6
7
ArrayName          WORD[0]            HINT[1]
  
Field 1 [0]        word1              hint1

Field 2 [1]        word2              hint2

Field 3 [2]        word3              hint3   



For eg. to retreive word1 you use WORDS[0][0] and for hint2, WORDS[1][1]

1
2
3
enum fields {WORD, HINT, NUM_FIELDS};
   const int NUM_WORDS = 5;
   const string WORDS[NUM_WORDS][NUM_FIELDS]

Maybe you are just confusing WORD in line 1 and WORDS in line 3.
Hi crazzyguy101 and thanks for the help.

I'm still a bit confused though. I guess what's really confusing me is how does the enumerated elements (word and hint) get created within the WORDS array as fields? Is it because NUM_FIELDS was used to create the specified dimension and it was the last element in the enumeration so everything before it is automatically stored? Meaning, were they automatically included during array dimension creation using the index of NUM_FIELDS as shown below?

const string WORDS[NUM_WORDS][NUM_FIELDS]

Also, from this bit of code:

1
2
string theWord = WORDS[choice][WORD]; // word to guess
string theHint = WORDS[choice][HINT]; // hint for word 


To retrieve a word and hint you use variables theWord and theHint which have been initialized to the array WORDS[choice][WORD] and WORDS[choice][HINT] which both are clearly using the enumerated elements (word and hint) to output the correct words and hints associated with each other. I need to know the technicalities of how this is working. I understand what the code does for program usage, but I don't understand how it's doing it.

I hope the way I'm asking makes sense because it doesn't make sense to me so I'm doing my best to explain it.

Thanks for the help all.
Enumerators are converted to ints when used where an int is expected. The array really doesn't know anything about fields. All it sees is this:

1
2
3
const string WORDS[5][2]
string theWord = WORDS[choice][0]; // word to guess
string theHint = WORDS[choice][1]; // hint for word  



Maybe you are a little confused about enumerators. You must understand that each element of an enumerator has an integer equivalent.

 
enum fields {WORD, HINT, NUM_FIELDS};

it is just like saying -
1
2
3
4
5
6
int WORD = 0;
int HINT = 1;
int NUM_FIELDS = 2;
// for eg, it is legal to say
int a = WORD - HINT;
int myArray[NUM_FIELDS];


Just think that all the values of WORD, HINT, and NUM_FEILDS are replaced by 0, 1 and 2.


http://www.cprogramming.com/tutorial/enum.html
Topic archived. No new replies allowed.