Hello
I'm a high school student, currently learning c++. I want to know if it's possible for me to access an online database (dictionary, etc.) that has many words I can draw from for a hangman game I have coded. As of now, I have the user input words into a text file from the main function. In a secondary void function, a word is randomly selected from that text file and used to play a game of Hangman in which you have 10 tries.
I'm not sure if I've been looking in the right places or not, but I haven't found anything online to help me with this issue. I'm not sure if it's too advanced or what, but I'm really interested in seeing if I can make this work.
Here is how I am currently retrieving pre-written words from the text file (bank.txt) if this means anything.
num = rand() % outcount + 1;
string snum = ""; // string which will contain the result
ostringstream convert; // stream used for the conversion
convert << num; // random num
snum = convert.str();
infile.open("bank.txt", ios::in);
if (infile.is_open())
{
while (!infile.eof())
{
getline(infile, stt, '#');
getline(infile, fileword);
if (stt.substr(0,100)==snum.substr(0,100))
{
origword = fileword; //origword is the word that will be used in the hangman game.
}//what this has done is select a random word from the text file, and using it in the hangman game.
}
}
not sure if this belongs in beginner or not, just really want some good advice on whether or not this is doable. Thanks :)
// return a list of words having at least min_chars characters each
// https://cal-linux.com/tutorials/vectors.html
std::vector<std::string> make_word_list( std::string path_to_word_file, std::size_t min_chars )
{
std::vector<std::string> word_list ;
std::ifstream file(path_to_word_file) ;
std::string word ;
while( file >> word ) if( word.size() >= min_chars ) word_list.push_back(word) ;
return word_list ;
}
// return a word randomly chosen from ones in word_list
std::string random_word( const std::vector<std::string>& word_list )
{
// consider using the C++ random number library instead
// http://en.cppreference.com/w/cpp/numeric/randomreturn word_list.empty() ? "" : word_list[ std::rand() % word_list.size() ] ;
}