Connecting Tech Pros Worldwide Forums | Help | Site Map

Help setting a hidden word

Newbie
 
Join Date: Apr 2007
Posts: 5
#1: Apr 21 '07
I am trying to write a hangman program that has only two functions other than main.

One of the functions:
void sethiddenword(char hidden[]);
is supposed to randomly assign a word to hidden. I have a list of 10 words i would like to use but can't use a control statment to set one of them into the array.

So far i have written
void sethiddenword(char hidden[]){
int rnum;
srand(time(0));
rnum = rand() %10;
}

This gives me the random number but i dont know how to assign the words to hidden. If you can do it with control statements i would like to see that. Either way, thanks.

ilikepython's Avatar
Expert
 
Join Date: Feb 2007
Posts: 839
#2: Apr 21 '07

re: Help setting a hidden word


Quote:

Originally Posted by mcdef

I am trying to write a hangman program that has only two functions other than main.

One of the functions:
void sethiddenword(char hidden[]);
is supposed to randomly assign a word to hidden. I have a list of 10 words i would like to use but can't use a control statment to set one of them into the array.

So far i have written
void sethiddenword(char hidden[]){
int rnum;
srand(time(0));
rnum = rand() %10;
}

This gives me the random number but i dont know how to assign the words to hidden. If you can do it with control statements i would like to see that. Either way, thanks.

Do you have a string array of 10 words? If yes, then you can have the index be the random number.
Expand|Select|Wrap|Line Numbers
  1. void sethiddenword(){     // no need to pass an array
  2.       int rnum;
  3.       srand(time(0));
  4.       rnum = rand() %10;
  5.       string hidden = wordlist[rnum]; //you can do it with strings
  6.       return hidden;
  7.                  or
  8.       char hidden[] = wordlist[rnum];       // or with char arrays
  9.       return hidden;
  10. }                                      
  11.  
That's what I did when I made my hangman. Also, you might want to change the function from a void to string return or a char array so you can return the word to main. I recommend using strings if you are using C++ but you can also do it with char arrays.
Newbie
 
Join Date: Apr 2007
Posts: 5
#3: Apr 21 '07

re: Help setting a hidden word


Quote:

Originally Posted by ilikepython

Do you have a string array of 10 words? If yes, then you can have the index be the random number.

Expand|Select|Wrap|Line Numbers
  1. void sethiddenword(){     // no need to pass an array
  2.       int rnum;
  3.       srand(time(0));
  4.       rnum = rand() %10;
  5.       string hidden = wordlist[rnum]; //you can do it with strings
  6.       return hidden;
  7.                  or
  8.       char hidden[] = wordlist[rnum];       // or with char arrays
  9.       return hidden;
  10. }                                      
  11.  
That's what I did when I made my hangman. Also, you might want to change the function from a void to string return or a char array so you can return the word to main. I recommend using strings if you are using C++ but you can also do it with char arrays.

I have to do it in just C i cant do it in C++ so i guess i have to use char arrays. I do not know what a word list is, but i have 10 words on paper that i can use, like "computer", and "hello", i do not know how to bring them into th program without doing something like.
char wordone[size] = "word"; 10 times.
ilikepython's Avatar
Expert
 
Join Date: Feb 2007
Posts: 839
#4: Apr 21 '07

re: Help setting a hidden word


Quote:

Originally Posted by mcdef

I have to do it in just C i cant do it in C++ so i guess i have to use char arrays. I do not know what a word list is, but i have 10 words on paper that i can use, like "computer", and "hello", i do not know how to bring them into th program without doing something like.
char wordone[size] = "word"; 10 times.

Well you can store the words in a text file and read them in to an array from the file. That can help when you decide to add more words.
Expand|Select|Wrap|Line Numbers
  1. int count = 0;    //used to store index in array
  2. ifstream file ("location_of_file");
  3. while (!file.eof()){
  4.     getline(file, wordlist[count]);     //wordlist is an array
  5.     count++;}   //increment index
  6. file.close();   //important to close file
  7.  
Hope that helps.
Newbie
 
Join Date: Apr 2007
Posts: 5
#5: Apr 21 '07

re: Help setting a hidden word


Quote:

Originally Posted by ilikepython

Well you can store the words in a text file and read them in to an array from the file. That can help when you decide to add more words.

Expand|Select|Wrap|Line Numbers
  1. int count = 0;    //used to store index in array
  2. ifstream file ("location_of_file");
  3. while (!file.eof()){
  4.     getline(file, wordlist[count]);     //wordlist is an array
  5.     count++;}   //increment index
  6. file.close();   //important to close file
  7.  
Hope that helps.

I dont think we are allowed to use file i/o. How do you set, inside of sethiddenword(), an array of char strings. Like if the array is hidden, how can i set 10 words to 1 array. ie. hidden[0] = "word1", hidden[1] = "word2", i know that this will not work because the number inside the array is the size of the array.
ilikepython's Avatar
Expert
 
Join Date: Feb 2007
Posts: 839
#6: Apr 21 '07

re: Help setting a hidden word


Quote:

Originally Posted by mcdef

I dont think we are allowed to use file i/o. How do you set, inside of sethiddenword(), an array of char strings. Like if the array is hidden, how can i set 10 words to 1 array. ie. hidden[0] = "word1", hidden[1] = "word2", i know that this will not work because the number inside the array is the size of the array.

You can declare the array like this:
Expand|Select|Wrap|Line Numbers
  1. char* hidden[10];
  2. //that makes it an array of char arrays
  3.  
then:
Expand|Select|Wrap|Line Numbers
  1. hidden[0] = "word1";
  2. hidden[1] = "word2";
  3. etc.
  4.  
Does that work for you?
Newbie
 
Join Date: Apr 2007
Posts: 5
#7: Apr 21 '07

re: Help setting a hidden word


Quote:

Originally Posted by ilikepython

You can declare the array like this:

Expand|Select|Wrap|Line Numbers
  1. char* hidden[10];
  2. //that makes it an array of char arrays
  3.  
then:
Expand|Select|Wrap|Line Numbers
  1. hidden[0] = "word1";
  2. hidden[1] = "word2";
  3. etc.
  4.  
Does that work for you?

if my function is
void sethiddenword(char hidden[]){
int rnum;
char *words[10];
srand(time(0));
rnum = rand() % 10;
printf("%d\n", rnum);
words[0] = "clemson";
words[1] = "tigers";
printf("%s\n", words[0]);
}
The printf stuff is just to see if my code worked. How do i assign the word to hidden[].

hidden[] = words[rnum]; Does not work?
ilikepython's Avatar
Expert
 
Join Date: Feb 2007
Posts: 839
#8: Apr 21 '07

re: Help setting a hidden word


Quote:

Originally Posted by mcdef

if my function is
void sethiddenword(char hidden[]){
int rnum;
char *words[10];
srand(time(0));
rnum = rand() % 10;
printf("%d\n", rnum);
words[0] = "clemson";
words[1] = "tigers";
printf("%s\n", words[0]);
}
The printf stuff is just to see if my code worked. How do i assign the word to hidden[].

hidden[] = words[rnum]; Does not work?

You can either try:
hidden = words[rnum];
and make the arguement from char hidden[] to char *hidden which I'm not sure will work,

or
have the function return the array like this:
Expand|Select|Wrap|Line Numbers
  1. char* sethiddenword(){
  2.     int rnum;
  3.     char *words[10];
  4.     srand(time(0));
  5.     rnum = rand() % 10;
  6.     words[0] = "clemson";
  7.     words[1] = "tigers";
  8.     char hidden[] = words[rnum];
  9.     return hidden;}
  10.  
Newbie
 
Join Date: Apr 2007
Posts: 5
#9: Apr 21 '07

re: Help setting a hidden word


Quote:

Originally Posted by ilikepython

You can either try:
hidden = words[rnum];
and make the arguement from char hidden[] to char *hidden which I'm not sure will work,

or
have the function return the array like this:

Expand|Select|Wrap|Line Numbers
  1. char* sethiddenword(){
  2.     int rnum;
  3.     char *words[10];
  4.     srand(time(0));
  5.     rnum = rand() % 10;
  6.     words[0] = "clemson";
  7.     words[1] = "tigers";
  8.     char hidden[] = words[rnum];
  9.     return hidden;}
  10.  

can i PM you what i have so far so you can get an idea of what my problem is.

I changed it to hidden = words[rnum] and that worked.
but when i call sethiddenword from my main function it doesnt not return the value of hidden.
ilikepython's Avatar
Expert
 
Join Date: Feb 2007
Posts: 839
#10: Apr 22 '07

re: Help setting a hidden word


Quote:

Originally Posted by mcdef

can i PM you what i have so far so you can get an idea of what my problem is.

I changed it to hidden = words[rnum] and that worked.
but when i call sethiddenword from my main function it doesnt not return the value of hidden.

Yea sure you can PM me
Reply