Please help me understand this part

Hi Everyone,

I want to start off by stating I decided to learn c++ as my first programming language. I understand not many experienced programmers recommend c++ for a complete beginner but I still decided to take on the challenge.

I started reading the book "C++ through game programming" by Michael Dawson. However, during my self-learning journey I find myself confused at the end of chapter 4 with the project Word Jumble. On the section "Jumbling the word" from my understanding the for loop is primarily used for counting or navigating through a sequence of elements. From my observation the value of i within the for loop is not indexed into any array and I don't understand how it is being used here. Also, the int index1 and index2 regenerates a random number from 0 to size number of the generated random word. Correct me if I'm wrong, but if the random chosen word is "wall" then it should be from 0-3 right? Does that mean that the w is considered 0 and the last l is 3? This whole section is very confusing, can anyone explain it to me? I understand the other areas but this part got me stuck. I will really appreciate your time and input towards this case. Thank you very much!

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
71
72
73
74
75
76
77
78
  //The classic word jumble game where the player can ask for a hint


#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;

int main()
{
	//Creating the Multidemnsional Array of Words

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

		{ "wall", "Do you fell 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" }
	};
	//Selecting random word 
	srand(static_cast<unsigned int>(time(0)));
	int choice = (rand() % NUM_WORDS);
	string theWord = WORDS[choice][WORD];
	string theHint = WORDS[choice][HINT];

	//Jumbling the word
	string jumble = theWord;
	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;
			
	}

	//Welcoming the player
	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;

	//Entering the game loop
	while ((guess != theWord)&(guess != "quit"))
	{
		if (guess == "hint")
		{
			cout << theHint;
		}
		else
		{
			cout << "sorry, that's not it";
		}
		cout << "\n\nYour guess: ";
		cin >> guess;
	}

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

	cout << "\nThanks for playing!.\n";
	return 0;
}
All the code in the for loop is doing is scrambling the letters of "theWord".

LightningBlaze wrote:
the for loop is primarily used for counting
Exactly what this loop is used for.

The modulo operator (http://www.cplusplus.com/doc/tutorial/operators/ - scroll down to arithmetic operators) will generate a number between 0 and Length-1.

To see what is happening try putting some cout statements inside the loop:
1
2
3
4
5
6
7
8
9
. . .
      jumble[index2] = temp;
      cout << i << "\t";
      for(int j = 0; j< Length; ++j){
          cout << jumble[j];
      }
      cout << endl;
  }
. . .
Thank you norm b!. Inserting also the cout command to display index1 and index2 after every loop helped me figured out how the process of jumbling the word took place!
Topic archived. No new replies allowed.