473,473 Members | 2,145 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Help setting a hidden word

5 New Member
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.
Apr 21 '07 #1
9 2476
ilikepython
844 Recognized Expert Contributor
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.
Apr 21 '07 #2
mcdef
5 New Member
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.
Apr 21 '07 #3
ilikepython
844 Recognized Expert Contributor
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.
Apr 21 '07 #4
mcdef
5 New Member
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.
Apr 21 '07 #5
ilikepython
844 Recognized Expert Contributor
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?
Apr 21 '07 #6
mcdef
5 New Member
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?
Apr 21 '07 #7
ilikepython
844 Recognized Expert Contributor
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.  
Apr 21 '07 #8
mcdef
5 New Member
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.
Apr 21 '07 #9
ilikepython
844 Recognized Expert Contributor
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
Apr 21 '07 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: baustin75 | last post by:
Posted: Mon Oct 03, 2005 1:41 pm Post subject: cannot mail() in ie only when debugging in php designer 2005 -------------------------------------------------------------------------------- ...
2
by: Catherine Jones | last post by:
Hi all, We are into the development of an application in C#. In one of the module we are using RichEdit control as text editor. This text editor is resposible for highlighting certain keywords....
10
by: Elizabeth Harmon | last post by:
Hi All I am attempting to open a Word App from a web page on the client and so far everything works (After reconfig of dcomcnfg for Microsoft Word Document). I have one minor problem, i cannot...
4
by: Rob | last post by:
Here is my issue: I am trying to load word through vb.net code behind an ASP.NET webform. I know it is possible to use the Automation and COM classes because a co-worker has it working on his...
1
hpbutterbeer
by: hpbutterbeer | last post by:
We have a Machine Project and my brain is currently in a clouded state. Sorry, I'm just a beginner in C Programming... Text twist is a windows game whose main objective is to form words out of the...
0
by: gunimpi | last post by:
http://www.vbforums.com/showthread.php?p=2745431#post2745431 ******************************************************** VB6 OR VBA & Webbrowser DOM Tiny $50 Mini Project Programmer help wanted...
0
by: magicofureyes | last post by:
Hello Guys im a just a new user and i dnt knw much abt Xml i want to upload a new template in Blogger so got some free coding but when i save this code in Blogger template it say '''' Your...
2
by: corykendall | last post by:
Here is my code: ... Setting hidden input to: <dsp:valueof param="tabname"/> <input name="tabname" type="hidden" value='<dsp:valueof param="tabname"/>'/> </dsp:form> hidden input set...
14
by: issentia | last post by:
I'm working on this site: http://www.essenceofsoy.com/redesign/index2.html and I'm having a few problems with getting the layout exactly right. 1) When the menu items are rolled over, they...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.