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.
9 2476
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. -
void sethiddenword(){ // no need to pass an array
-
int rnum;
-
srand(time(0));
-
rnum = rand() %10;
-
string hidden = wordlist[rnum]; //you can do it with strings
-
return hidden;
-
or
-
char hidden[] = wordlist[rnum]; // or with char arrays
-
return hidden;
-
}
-
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.
Do you have a string array of 10 words? If yes, then you can have the index be the random number. -
void sethiddenword(){ // no need to pass an array
-
int rnum;
-
srand(time(0));
-
rnum = rand() %10;
-
string hidden = wordlist[rnum]; //you can do it with strings
-
return hidden;
-
or
-
char hidden[] = wordlist[rnum]; // or with char arrays
-
return hidden;
-
}
-
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.
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. -
int count = 0; //used to store index in array
-
ifstream file ("location_of_file");
-
while (!file.eof()){
-
getline(file, wordlist[count]); //wordlist is an array
-
count++;} //increment index
-
file.close(); //important to close file
-
Hope that helps.
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. -
int count = 0; //used to store index in array
-
ifstream file ("location_of_file");
-
while (!file.eof()){
-
getline(file, wordlist[count]); //wordlist is an array
-
count++;} //increment index
-
file.close(); //important to close file
-
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.
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: -
char* hidden[10];
-
//that makes it an array of char arrays
-
then: -
hidden[0] = "word1";
-
hidden[1] = "word2";
-
etc.
-
Does that work for you?
You can declare the array like this: -
char* hidden[10];
-
//that makes it an array of char arrays
-
then: -
hidden[0] = "word1";
-
hidden[1] = "word2";
-
etc.
-
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?
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: -
char* sethiddenword(){
-
int rnum;
-
char *words[10];
-
srand(time(0));
-
rnum = rand() % 10;
-
words[0] = "clemson";
-
words[1] = "tigers";
-
char hidden[] = words[rnum];
-
return hidden;}
-
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: -
char* sethiddenword(){
-
int rnum;
-
char *words[10];
-
srand(time(0));
-
rnum = rand() % 10;
-
words[0] = "clemson";
-
words[1] = "tigers";
-
char hidden[] = words[rnum];
-
return hidden;}
-
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.
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
Sign in to post your reply or Sign up for a free account.
Similar topics |
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
--------------------------------------------------------------------------------
...
|
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....
|
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...
|
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...
|
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...
| |
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...
|
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...
|
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...
|
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...
|
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,...
|
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,...
| |
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...
|
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...
|
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,...
|
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...
|
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 ...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |