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
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.
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))];
}
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!!
Re: Need Some Quick help Please!
Quote:
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.
Re: Need Some Quick help Please!
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 :)
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 :D
Re: Need Some Quick help Please!
Re: Need Some Quick help Please!
I just simply posted this thread in the c++ forum.
JT