Arrays storing data to be eliminated?

I wondered if it was possible to store data in an array so that a program could search through the array and NOT use certain data if it has been used before. For example, if one were to create a program where a player is presented with a jumbled word from an array and had to guess the word, and the game contained say 100 words and 10 rounds, would it be possible for the program to log it's choice of word in an array and then at the start of each round search through the array to see if it's choice has been used before and not use that choice if it had been previously used?

To clarify further, the words that are selected are selected at random and so reducing the word list and ensuring they appear in order rather than randomly would not be ideal as it would produce the same words each time the game is played. On another note, I considered it might be possible for the program to select a random index in the array and display these 10 choices starting from that index ensuring that the program will at least be more likely to start on a different word each time but feel even that isn't random enough. It's a solution that I'll accept but if there's a better one I'd rather hear what others think about it.

On a side-note, would it be possible to use a similar technique in a program that guessed a user's choice of number to ensure the program didn't guess the same number twice and to ensure that the guess falls within a range set by the user. By this I mean if the guess 30 is too low and the guess 32 too high would there be an expression that ensured the guess falls between these values which are stored in an array?

All opinions welcome and thanks in advance!
There are a lot of ways to do this. Probably the simplest way is to randomize the order of the words in the array
once at the beginning of the problem, then in the first round, use the 0th word, in the second round use the 1st,
in the third round use the 2nd, and so forth. You can use std::random_shuffle() to randomize the order of the words.

Another easy way is this: In the first round choose a random number between [ 0... word list size - 1 ], and use
that word. Then, swap that word with the last word in the list. In the second round, choose a random number
between [ 0 ... word list size - 2 ], and use that word. Then, swap that word with the second to last word in
the list. In this way, the first N words in the list are always unused and the last M words are always used.
Interesting...

So in your first scenario you would implement the randomisation function outside of the game loop and rather than randomising a single word it would randomise the whole list, correct? That seems a very simple and elegant way of doing it as it ensures the words are always random and never repeated. So tu use the random shuffle function with an array called WORDS one would, at the start of a program simply use:

answer = WORDS.random_shuffle()

and then to implement it one would use a for loop like so:

1
2
3
4
5
for (int i = 0; ; ++i)
{
cout << "The word is: " << WORDS[i];
...
}


Is that about right?
Can anyone confirm, deny or correct this?
No. Arrays are not a class, hence they have no member functions. The correct way to use random_shuffle:

std::random_shuffle(words, words+SIZE);
So this is what I have based on what has been said being included in an existing program. I have implemented the random_shuffle() function to shuffle the words in the array (I'm not sure if the second argument passed should be [NUM_FIELDS - 1] or just as I have it down) but I am getting an odd error that I can't quite find. Never had it before but the only line reference it gives is (103) and I don't have that many lines. It says:

c:\program files\microsoft visual studio 10.0\vc\include\utility(103): error C2678: binary '=' : no operator found which takes a left-hand operand of type 'const std::string' (or there is no acceptable conversion)


I'm not sure which = operator it's referring to but it could be the expressions at lines 41 and 42. How do I go about correcting this program?

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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include <iostream>
#include <string>
#include <algorithm>
#include <cstdlib>
#include <ctime>

using namespace std;

int main()
{
	string guess;
	enum fields {WORD, HINT, NUM_FIELDS}; //enumeration sets const objects inside variable field and
	const int NUM_WORDS = 10;			  //NUM_FIELDS holds number of fields in array as index is 2//
	const string WORDS[NUM_WORDS][NUM_FIELDS] = //array sets up
	{
		{"wall", "Do you feel like you're banging your head against something?"},
		{"glasses", "These might help you see the answer."},
		{"laboured", "Going slowly, is it?"},
		{"persistent", "Keep at it!"},
		{"jumble", "It's what the game is all about."},
		{"banana", "Careful you don't slip up."},
		{"style", "By what you're wearing I can see you have none of this."},
		{"life", "Sitting here playing this proves you don't have one of these."},
		{"idiot", "If you neede a hint for this one you're one of these."},
		{"books", "You should read a few, might help your jumbling."}
	};

	cout << "\t\tWelcome to Word Jumble!\n\n";
	cout << "Unscramble the letters to make a word.\n";
	cout << "10 points for every letter in the word, -10 points for using a hint.\n";
	cout << "Enter 'hint' for a hint.\n";
	cout << "Enter 'quit' to quit the game.\n\n";

	int score = 0;
	int round = 1;
	random_shuffle(WORDS[0], WORDS[NUM_WORDS]);

	do //sets up game loop until player types "quit"
	{

	string theWord = WORDS[round - 1][WORD];
	string theHint = WORDS[round - 1][HINT];

	string jumble = theWord;
	int length = jumble.size();
	for (int i = 0; i < length; ++i)
	{
		srand(time(0));
		int index1 = (rand() % length);
		int index2 = (rand() % length);
		char temp = jumble[index1];
		jumble[index1] = jumble[index2];
		jumble[index2] = temp; //sets up jumbled version of word by picking random letters and swapping
	}							//them as many times as there are characters in the word//

	int roundScore = theWord.length() * 10; //decides score for word//

	cout << "Round: " << round << "\n";
	cout << "Your score is: " << score << "\n";
	cout << "This word is worth: " << theWord.length() * 10 << " points.\n";
	cout << "The jumble is: " << jumble;

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

	while ((guess != theWord) && (guess != "quit"))
	{
		if (guess == "hint")
		{
			cout << theHint;
			roundScore = (theWord.length() * 10) - 10; //deducts points for using hint//
		}
		else
			cout << "Sorry, that's not it.";

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

	if (guess == theWord)
	{
		cout << "\nThat's it! You guessed it!\n\n";
		score = score + roundScore; //adds score for this round to total score//
		++round;
	}
	}
	while (guess != "quit"); //ends loop if player enters quit//

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

	return 0;
}
Anyone?
Ugh, use a struct instead of a 2D array:

1
2
3
4
struct Entry {
    std::string word;
    std::string hint;
};


Then:

1
2
3
Entry entries[ MAX_ENTRIES ] = { ... };

std::random_shuffle( entries, entries + MAX_ENTRIES );


Otherwise with a 2D array you're writing your own random_shuffle.
Topic archived. No new replies allowed.