random words

Started by
14 comments, last by da_cobra 22 years, 6 months ago
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...
Advertisement
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
2TORNADO-
3CATTLE--
4FORMAT--

I hope you can understand this, ''cause I think I suck at explaining things :D
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...
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.
I own the bad habit of calling a 2d-array a matrix :D
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''  


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

again thanx in advance

Edited by - da_cobra on October 14, 2001 12:49:57 PM
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...
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;} 
what parameters are that : "int argc, char *argv[]" ?!?

This topic is closed to new replies.

Advertisement