|
-
Nov 15th, 2005, 07:42 AM
#1
Thread Starter
New Member
Need Some Quick help Please!
Hey guys, I was needing some help on a portion of a program I am writing for my cs class. We are designing a hangman game. I have a lot of my functions built already and pretty much have it all undercontrol, but one thing. Before I say anything else, no I do not expect anyone to write this whole program for me, but I do need help on this one function. I need to write a function that returns a random word out of a file. I have tried using rand and srand, but can't figure out how to do this. I have my file built already with 8 words in it, however I just can't figure out how to generate a function that will pull one of these words out randomly. I would really appreciate if anyone could shed some light on this and help me figure this out. Thanks!!
JT
-
Nov 15th, 2005, 08:12 AM
#2
Lively Member
Re: Need Some Quick help Please!
If you read each word into an array, you can call a random integer, set it to a variable (for example, x) and call strListOfWords[x] or something.
-
Nov 15th, 2005, 08:19 AM
#3
Re: Need Some Quick help Please!
Code:
string RandomWord(string fileContents)
{
string[] words = fileContents.Split(" ");
Random rand = new Random(DateTime.Now.Milliseconds);
return words[rand.Next(0, words.GetUpperBound(0))];
}
-
Nov 15th, 2005, 08:32 AM
#4
Thread Starter
New Member
Re: Need Some Quick help Please!
Thanks fellas. I really appreciate your input. The only thing with arrays is that we haven't even studied them in class and I have no clue how to use them. Pengate, that looks great, I will try that, but I am a little confussed, it looks a little different. Like, what would go in the parentheses where is says "string filecontents"?? I appologise for my stupidity in c++ but I really just don't get this stuff very well, I really have to work at it. I do appreciate you all giving me some help though. Any more help is well appreciated as well. I need all the help I can get. Thanks!!
-
Nov 15th, 2005, 08:35 AM
#5
Re: Need Some Quick help Please!
I appologise for my stupidity in c++
Are you using C++, or C#? The two are completely different.
Between the parentheses after the method name are the function arguments. These are parameters that you pass to the function that the function can use.
-
Nov 15th, 2005, 08:46 AM
#6
Thread Starter
New Member
Re: Need Some Quick help Please!
-
Nov 15th, 2005, 08:50 AM
#7
Re: Need Some Quick help Please!
That's OK... Send a private message to Hack and ask him to move this thread to the C++ forum 
Unfortunately, I don't know enough about the C++ standard library to be able to help you do it in that language, but we have some C++ experts who will
-
Nov 15th, 2005, 09:10 AM
#8
Re: Need Some Quick help Please!
OK, how good this is I'm not sure, but it might get you started.
Code:
#include <cstdlib>
#include <ctime>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
string RandomWord (string fileContents)
{
string buffer;
stringstream stream(fileContents);
vector<string> tokens;
while (stream >> buffer)
{
tokens.push_back(buffer);
}
srand(time(NULL));
return tokens[rand() % (tokens.size() - 1)];
}
Bear in mind that you shouldn't take anything I say about C++ as gospel
-
Nov 15th, 2005, 11:58 AM
#9
Thread Starter
New Member
Re: Need Some Quick help Please!
-
Nov 15th, 2005, 12:04 PM
#10
Thread Starter
New Member
Re: Need Some Quick help Please!
I just simply posted this thread in the c++ forum.
JT
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|