#### Archived

This topic is now archived and is closed to further replies.

# random words

This topic is 5934 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I''m trying to make a simple hangman game, I already made functions that read the length of a string and search for a letter in it, but now I would like to know how I could let the program pick a random word (from a file, or a matrix) I already figured out that I will have to use a random number that points to a random word, but where do I put those words I can''t use a string matrix, because a number would only point to a letter of a word, or am I wrong?!? can someone help me thanx in advance...

##### Share on other sites
I guess using a char matrix would be easiest.

Horizontal: each variable would hold one letter, or a null to indicate the end of the word.

Vertical: the seperate words are filled in under each other. You should create a random number between 0 and n (number of words). That random number would be the vertical indicator in your matrix.

ASCII:
x12345678
0DOG-----
1AIRPLANE
3CATTLE--
4FORMAT--

I hope you can understand this, ''cause I think I suck at explaining things :D

##### Share on other sites
do you mean a 2 dimensional array?

like :

char array [5] [10]
{
{0,1,2,3,4,5,6,7,8,9}
{1,''w'',''o'',''r'',''d'',''\0''}
{2,''d'',''o'',''g'',''\0''}
{3,........}
{4,......}
} ;

if so I don''t think it''s easy to use...

##### Share on other sites
Look up the strtok() function. It extracts substrings delimited by a specified set of separators. You could, for example, specify all whitespace (the space character, tabs, newlines, etc) as your separators and use strtok to extract words from any text file.

##### Share on other sites
I own the bad habit of calling a 2d-array a matrix :D

##### Share on other sites
quote:
Original post by da_cobra
do you mean a 2 dimensional array?

like :

char array [5] [10]
{
{0,1,2,3,4,5,6,7,8,9}
{1,''w'',''o'',''r'',''d'',''\0''}
{2,''d'',''o'',''g'',''\0''}
{3,........}
{4,......}
} ;

if so I don''t think it''s easy to use...

You can do it like you wrote, only loose the first row, and the initial number in each row (Ronin_54 only had them to show column- and row-numbers, they were not supposed to be in the strings)

for example:
  const int NUMBER_OF_WORDS = 3;const int MAX_WORD_LENGTH = 10;char word_list[NUMBER_OF_WORDS] [MAX_WORD_LENGTH] ={ "tornado", "dog", "cattle" // you get the point}; // then to get a random string from array, do:int word_index = rand() % NUMBER_OF_WORDS;char * word = word_list[word_index]; // now ''word'' is a pointer to a word in the array ''word_list''

##### Share on other sites
I think that last one could work, so now I try that to put it in a function like this :

void newword(char *word) ;

void main()
{
char word[10]="" ;
newword(word) ;
cout << word << endl ;
}

void newword(char *word)
{
int const words=5 ;
int const max=10 ;
int a=0 ;
char list [words] [max] =
{
"word", "car", "house", "dog", "cat"
} ;
int word_index = rand() % words ;
word = list[word_index];
}

but my word stays "" ?!?

what am I doing wrong now?
and no I can't use : *word=list[word_index] ;
because then I get the following error :
error C2440: '=' : cannot convert from 'char [10]' to 'char'
This conversion requires a reinterpret_cast, a C-style cast or function-style cast

Edited by - da_cobra on October 14, 2001 12:49:57 PM

##### Share on other sites
YOu aren''t storing an entire word in your variable now

Either figure out how to use the pointer correctly, or read every single character of your word into a seperate array entry (ie. word[10]) till you get a NULL...

##### Share on other sites
quote:
Original post by da_cobra
void newword(char *word)
{
int const words=5 ;
int const max=10 ;
int a=0 ;
char list [words] [max] =
{
"word", "car", "house", "dog", "cat"
} ;
int word_index = rand() % words ;
word = list[word_index];
}

1.) You create your wordlist every time you need a random word, which is bad design.
2.) Yo declare word to be a constant array of chars, which you don''t want. You want a pointer, so there''s no data copying.

// note that these are global; they are declared outside *any* functionconst int MAX_WORDS = 5;const int MAX_CHARS = 10;// 2D array of chars:char word_list[MAX_WORDS][MAX_CHARS] ={  "word",  "car",  "house",  "dog",  "cat"};int main(int argc, char *argv[]){  int word_index = rand() % MAX_WORDS;  // note that word is a char *, not char[10]  char *word = word_list[word-index];  cout << word << endl;}

##### Share on other sites
what parameters are that : "int argc, char *argv[]" ?!?

##### Share on other sites
Those are the usual arguments for main, but you can overload main to take no parameters: int main(void).

int argc - the number of command line parameters (arg ument c ount). Always >= 1, since the first argument is the program name.
char *argv[] - an array of strings (which are arrays of chars) (arg ument v alues). argv[0] is the program name (1st parameter) and so on.

##### Share on other sites
Oluseyi wrote:

// note that these are global; they are declared outside *any* function
const int MAX_WORDS = 5;
const int MAX_CHARS = 10;

Never do this, because global variables are wanted to be avoided. Not only do they use up a lot more space, but they can be changed by any function, which eliminates the purpose of encapsulation in C++. You may, however, define MAX_WORDS and MAX_CHARS as follows:

#define MAX_WORDS 5
#define MAX_CHARS 10

What this does is it looks in your entire program before compiling and replaces wherever it finds MAX_WORDS with 5. Same with MAX_CHARS. Notice also that there is no '';'', because you only want to replace a number, not a statement. These statements should go right below your #include directives.

You may also define a constant integer, like above, but you must pass it to all the functions that will use it, and it will not be able to be changed. The last method is becomming more and more popular.

-Ace

##### Share on other sites
quote:
Original post by ace_muncher
Oluseyi wrote:

// note that these are global; they are declared outside *any* function
const int MAX_WORDS = 5;
const int MAX_CHARS = 10;

Never do this, because global variables are wanted to be avoided. Not only do they use up a lot more space, but they can be changed by any function, which eliminates the purpose of encapsulation in C++.

That's not true. Since the globals MAX_WORDS and MAX_CHARS are declared with the 'const' keyword they are constants. This means that the program is not allowed to change them. And in the case of an optimizing compiler their values will be inserted directly into the code (just like with a macro).

Declaring consts is actually to prefered to declaring macros.
quote:

You may also define a constant integer, like above, but you must pass it to all the functions that will use it, and it will not be able to be changed.

No, you won't have to pass it to every function that uses it, because it is declared in the global scope (just make sure it is declared *before* any function that uses it).

Edited by - Dactylos on October 15, 2001 12:44:27 AM

##### Share on other sites
Guys, your killing yourselves with trying to define the bounds of the array. If you know what the array will be at compile time, you don't have to give it bounds.

  const char *cStringArray[] = { "The cat ran up the hill", "the cow jumped over the moon", "there's a hole...5...10...he's running over people! Oh you Herschel Walker!", ""};void function(void){ int NumStrings; char *cRandomString; // find the number of strings for (NumStrings= 0; cStringArray[NumStrings][0] != 0; NumStrings++) {} // now NumStrings is the number of strings in array cRandomString = cStringArray[rand()%NumStrings]; // now you have a random string in cRandomString}

Edited by - BeerNutts on October 17, 2001 2:11:14 PM

##### Share on other sites
quote:
Original post by da_cobra
I think that last one could work, so now I try that to put it in a function like this :

void newword(char *word) ;

void main()
{
char word[10]="" ;
newword(word) ;
cout << word << endl ;
}

void newword(char *word)
{
int const words=5 ;
int const max=10 ;
int a=0 ;
char list [words] [max] =
{
"word", "car", "house", "dog", "cat"
} ;
int word_index = rand() % words ;
word = list[word_index];
}

but my word stays "" ?!?

what am I doing wrong now?
and no I can''t use : *word=list[word_index] ;
because then I get the following error :
error C2440: ''='' : cannot convert from ''char [10]'' to ''char''
This conversion requires a reinterpret_cast, a C-style cast or function-style cast

Edited by - da_cobra on October 14, 2001 12:49:57 PM

This is almost correct. You just need to include string.h at the top of your program, and replace

word = list[word_index];

with

strcpy(word, list[word_index]);