Help understanding something please | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Help understanding something please

Im reading through a book that has a word jumble program that I don't understand a part of if anyone can explain. 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."} }; 1. First I don't quite understand what ENUM is? 2. I understand that this is an array made of strings and I understand that Const int num_words=5 means that there are 5 possible words to unscramble. Meaning a 5 row array. 3. I understand that the array is called WORDS. and that it is [num_words] or 5 rows and [num_fields] is the number of columns. But no where does it set the number of columns? Should there not be a num_fields=2;? Later... 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 I understand the 1st two lines. Set a random number with a seed of time and random number is between 0 and 4 based on number of words. but for string theHint=WORDS[choice][HINT] no where was an array with [HINT] established? Where is WORDS[num_words][num_fields]? This makes no sense to me please help.

6th Jun 2017, 5:41 PM
Bryan
2 Answers
+ 2
Supplemental material for the book I believe you have is linked below....(not everyone knows how to find it/was looking for errata) Reviewing the source [jumble.cpp in chapter 3] I feel more comfortable answering... Enums (enumerations...enumerate -- or "number") assign sequential integer values to a list of identifiers (here, CONSTANTS; that's why they're CAPITALIZED) so you can just use the names instead of numbers like 0, 1, 2, etc. So...automatically: WORD=0, HINT=1, NUM_FIELDS=2 And... WORDS[choice][HINT] is the same as writing: WORDS[choice][1] but easier to understand for humans. Book links (source code, data files, etc): Beginning C++ Through Game Programming / Michael Dawson 3rd Edition http://www.delmarlearning.com/companions/index.asp?isbn=1598633600 4th Edition http://www.delmarlearning.com/companions/index.asp?isbn=1435457420 5th Edition http://www.delmarlearning.com/companions/index.asp?isbn=1305109910
6th Jun 2017, 7:22 PM
Kirk Schafer
Kirk Schafer - avatar
+ 1
thank you kirk
6th Jun 2017, 7:23 PM
Bryan