Hi guys, first post so be nice :S
I've been working on a software project (wordsearch generator) for one of my classes at university and I'm stumped on this section. I'm supplied with an array filled with x strings, the integer value of x (for the size of the array) and an integer specifying how many strings to be used.
The first task is to randomly select the specified amount of strings. Here is the code I have so far;
#include <ctime>
#include <cstdlib>
#include <string>
#include <iostream>
using namespace std;
int main()
{
//*************Inputs from other section*****************//
string wordbank[]={"yes", "no", "hello", "goodbye", "france", "google", "diagram", "strath", "x", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "getting", "close", "kabam"};
int no_of_words = 10;
int words_in_bank = 20;
//*************Inputs from other section*****************//
int array_element; //defines the element of wordbank[] array to be accessed
int check_array[]={-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1}; //array used to check whether duplicates are created
int t; //looping variable
int chosen_words; //indicates the number of words chosen
int flag; //variable used to indicate duplicate words
chosen_words = 0; //initialise variable to zero
while(chosen_words < no_of_words) //loop will terminate once the correct number of words are selected
{
array_element = rand() % words_in_bank; //and set this to array_element
for(t=0; t<20; t++) //loop through each element of the array
{
flag = 0;
if (check_array[t] == array_element) // if match is found
{
flag = 1;
break;
}
else
{
if (check_array[t] == -1)
{
check_array[t] = array_element;
}
}
}
if (flag != 1)
{
cout << wordbank[array_element] << endl;
chosen_words++;
}
}
return 0;
}
I've tried to be specific with my comments although if you don't understand my logic (?) feel free to ask!! It's giving me the following output;
no
strath
fifteen
yes
ten
france
close
close
hello
france
Press any key...
I'm guessing it's something within the if statement which is allowing the same index "array_element" pass. I've tried everything I can, any and all help appreciated :)
Pete