+ 2

Help please.

https://code.sololearn.com/cfEeOrJB5IT5/#cpp In this code I do not understand the following section. int main() { 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", "It's what the game is all about."} }; srand(static_cast<unsigned int>(time(0))); int choice = (rand() % NUM_WORDS); string theWord = WORDS[choice][WORD]; // word to guess string theHint = WORDS[choice][HINT]; // hint for word If the array is called WORDS[num_words][num_fields] then how are they calling the hint by saying WORDS[choice][HINT] I understand how choice was gotten but I don't understand hint. No where was hint defined. HINT is not part of the array wasn't the array [num_fields] Also how is WORDS[choice][WORD] work? Wasn't that supposed to be [num_words]

6th Jun 2017, 6:41 PM
Bryan
5 Antworten
+ 5
HINT is defined in the enum. It's value is 1 so it always prints whatever the description of the hint was. Its grabbing a value from the array to print to the console. The WORDS array doesn't look like it's being used as intented. Could just be a one dimensional array.
6th Jun 2017, 6:54 PM
Rrestoring faith
Rrestoring faith - avatar
+ 4
Yes. But choice can only be from 0-4
6th Jun 2017, 7:01 PM
Rrestoring faith
Rrestoring faith - avatar
+ 3
HINT is part of an enumeration. An enum is basically a list of constants whose values are in succession of one another. if the values are not explicitly defined, it will start at 0, then 1, and so on. So for enum fields {WORD, HINT, NUM_FIELDS} WORD is 0, HINT is 1, and so on.. It would be similar to using define macros like #define WORD 0 #define HINT 1 I haven't thoroughly looked over this code to tell you its usage but enums are useful for switch conditionals. switch (choice) { case WORD: break; }
6th Jun 2017, 6:54 PM
Sean Patrick Franklin
Sean Patrick Franklin - avatar
+ 1
Right 0-4 and that's why in the rand()%NUM_WORDS they didn't do +1 like you would do if you wanted a random number between 1-5
6th Jun 2017, 7:03 PM
Bryan
0
So I think I understand now. The ENUM is simply assigning the number 0,1,2 and since they have the array set as WORD[NUM_WORDS][NUM_FIELDS] that is the equivalent as simply saying WORD[5][2] but when they call cout<< WORDS[CHOICE][HINT] its the same as calling cout<<WORDS[1-5][1] In other words it is always calling a value in the second column but varying the row? Is that the correct understanding of it?
6th Jun 2017, 7:00 PM
Bryan