Quantcast

Jump to content

» «
Photo

C++ Code Help

4 replies to this topic
Indi
  • Indi

    "Party's over, kid!"

  • Andolini Mafia Family
  • Joined: 22 Nov 2008
  • None

#1

Posted 29 January 2013 - 04:15 AM

Hey guys/gals,

CODE
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", "It's 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


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


Work with this:
CODE
enum fields {WORD, HINT, NUM_FIELDS};
const int NUM_WORDS = 5;
const string WORDS[NUM_WORDS][NUM_FIELDS]


It's supposed to be a "Word Jumble" game but this C++ book I have hasn't explained it very well on this part. Could someone possibly walk through it with me on how like the enums, const values and stuff work with the int choice, string theWord and string theHint? Thanks.

K^2
  • K^2

    Vidi Vici Veni

  • Moderator
  • Joined: 14 Apr 2004
  • United-States
  • Most Knowledgeable [Web Development/Programming] 2013
    Most Knowledgeable [GTA Series] 2011
    Best Debater 2010

#2

Posted 29 January 2013 - 08:01 AM

What isn't clear? There are quite a few concepts there. Do you understand what enum does? Do you understand how arrays work in C/C++? Do you understand srand() and rand() functions?

If none of it is making sense, you probably should start earlier in the book. If you have specific questions, please, ask specific questions.

Indi
  • Indi

    "Party's over, kid!"

  • Andolini Mafia Family
  • Joined: 22 Nov 2008
  • None

#3

Posted 29 January 2013 - 02:19 PM

Yeah I understand all of the concepts. Enumerators can be used to hold data so that the user can be notified on something like a difficulty level in a game, srand seeds a pseudorandom number from memory within the system, rand gets that number from srand, arrays are a list of elements that can be called upon but are not infinite.

The thing I don't get is the NUM_WORDS = 5 and the other NUM_WORDS which is in string WORDS. Are they both the same or are they different? Another thing about int choice is how does it select the random word? My brain is thinking its using 5 but I'm not sure if it is.

To clarify then I want to know which part of the code gets the random word and how does it work.

Thanks.

Rawra
  • Rawra

    Daedric Prince of Madness

  • Members
  • Joined: 06 Aug 2007

#4

Posted 29 January 2013 - 08:49 PM Edited by Rawra, 29 January 2013 - 08:52 PM.

Hi Indi,

CODE
const int NUM_WORDS = 5;

NUM_WORDS is an integer with size 5
enumerators have words which correspond to integer values in order from 0 (unless you specify otherwise)

CODE
enum fields {WORD, HINT, NUM_FIELDS}

WORD = 0, HINT = 1, NUM_FIELDS = 2

so printing out NUM_FIELDS in this case is the same as printing out the value 2

another example is
CODE
enum test { T1, T2, T3, T4};

T1 has value 0, T2 has value 1 etc



CODE
const string WORDS[NUM_WORDS][NUM_FIELDS]


makes a 2D array of strings, NUM_WORDS by NUM_FIELDS big
NUM_WORDS was given the value 5 and NUM_FIELDS is 2 as I said above,

so in this case it is the same as
CODE
const string WORDS[5][2]



CODE
srand(time(0))

sets the time as the seed for the new random numbers (IIRC)

CODE
int choice = (rand() % NUM_WORDS)

gets a random number and sets it between 0 and 4
the % gets the remainder when the random number is divided by 5 (NUM_WORDS)


so by this stage, 'choice' now has a value between 0 and 4

CODE

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


assume choice is given the value 1

theWord = WORDS[1][WORD]

WORD = 0, so theWord = WORDS[1][0] which corresponds to the first string in the 2nd row which is 'glasses'

theHint = WORDS[1][HINT]
HINT = 1, so theHint = WORDS[1][1] which corresponds to the second string in the 2nd row which is "These might help you see the answer."



Again as K^2 said if this makes no sense still then you should start again. I personally recommend C++ Primer by Stephen Prata which is what I used to learn C/++


Edit:

Also, a const value is just a constant value which can never change once defined.
So doing
CODE
const int x = 5; x = 3;
is an error

Indi
  • Indi

    "Party's over, kid!"

  • Andolini Mafia Family
  • Joined: 22 Nov 2008
  • None

#5

Posted 29 January 2013 - 09:21 PM

Crystal clear.

Thank you. I forgot that the last value in an enumerator can be used to hold how many elements are in that enumerator.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users