Understanding World Jumble Program.

May 23, 2014 at 4:07am
Can somebody please help me understand how this program exactly works?
I'm trying to understand how does it know which is a hint and which is the answer.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>

using namespace std;

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

    string jumble = theWord;  // jumbled version of word
    int length = jumble.size();
    for (int i=0; i<length; ++i)
    {
        int index1 = (rand() % length);
        int index2 = (rand() % length);
        char temp = jumble[index1];
        jumble[index1] = jumble[index2];
        jumble[index2] = temp;
    }

    cout << "\t\t\tWelcome to Word Jumble!\n\n";
    cout << "Unscramble the letters to make a word.\n";
    cout << "Enter 'hint' for a hint.\n";
    cout << "Enter 'quit' to quit the game.\n\n";
    cout << "The jumble is: " << jumble;

    string guess;
    cout << "\n\nYour guess: ";
    cin >> guess;

    while ((guess != theWord) && (guess != "quit"))
    {
        if (guess == "hint")
		{
            cout << theHint;
		}
        else
		{
            cout << "Sorry, that's not it.";
		}

        cout <<"\n\nYour guess: ";
        cin >> guess;
    }

    if (guess == theWord)
	{
        cout << "\nThat's it!  You guessed it!\n";
	}

    cout << "\nThanks for playing.\n";

    return 0;
}


This code is from Chapter 3 known as the Word Jumble program. When you type in hint, it gives you a hint from the word that is "jumbled" but exactly how does it know that? Thank you.
May 23, 2014 at 4:28am
line 49 checks if the word you entered is 'hint'.
May 23, 2014 at 4:51am
yea, but i mean how does it know to print out the hint? how does it know that line 15-18 has a word, and when you type hint how does it know to pull out the next work on line 15-18
May 23, 2014 at 5:25am
lines 12-19 define a 2d array of strings called WORDS. A random word and hint is chosen from this array in lines 21-24. It just displays the hint chosen by those lines.
May 23, 2014 at 5:50am
I'm not really understanding how exactly everything comes together. i'm not sure why the enum fields really exist...
May 23, 2014 at 5:54am
maybe im more confuse to the fake of why the enum is there, but also why Num_fields is also there...
May 23, 2014 at 5:59am
An enum is a way to set the values of multiple ints at once.
1
2
3
4
5
enum fields {WORD, HINT, NUM_FIELDS};
// is the same as
int WORD = 0;
int HINT = 1;
int NUM_FIELDS = 2;
Topic archived. No new replies allowed.