Read words from file, choose one then output
Jun 5, 2013 at 12:50am Jun 5, 2013 at 12:50am UTC
Ok so i want the program to be able to read a list of words from a file and then randomly choose one then output it all jumbled up. I already know how to jumble them up but i dont know how to input the list then randomly choose one. Heres what i got so far
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
#include <iostream>
#include <string>
#include <algorithm>
#include <ctime>
#include <cstdlib>
#include <fstream>
#include <sstream>
using namespace std;
void word_list()
{
string store_list = " " ;
ifstream fileIn;
fileIn.open("word list.txt" );
stringstream ss(stringstream::in | stringstream::app | stringstream::out);
}
int main()
{
word_list();
srand(time(0));
string original_word = " " ;
string shuffled_word = " " ;
string guess = " " ;
bool game_loop = true ;
cout << "CRYPTO GUESS\n" << endl;
//Main Game loop
while (game_loop != false )
{
cout << "Please enter your word" << endl;
cin >> original_word;
shuffled_word = original_word; //#1
random_shuffle(shuffled_word.begin(), shuffled_word.end()); //#2
for (int i = 0; i < 300 ; i++)
{
cout << "\n" ;
}
cout << shuffled_word << endl;
cout << "\n" ;
while (guess != original_word)
{
cout << "Your guess: " ;
cin >> guess;
if (guess == original_word)
{
cout << "Is correct!\n" << endl;
break ;
}
else if (guess == "Quit" || guess == "quit" )
{
cout << "Goodbye thanks for playing" << endl;
return 0;
}
else
{
cout << "Is wrong! Try again\n" << endl;
}
}
}
}
Jun 5, 2013 at 2:23am Jun 5, 2013 at 2:23am UTC
does anyone know?
Jun 5, 2013 at 4:34am Jun 5, 2013 at 4:34am UTC
I may be able to help you out. I just recreated your game for fun, and you can check my source to see what I used to make it work.
So to answer your question "i dont know how to input the list then randomly choose one"...
To input the list of words I used std::ifstream (starting on line 17) to open the word list file, then copied the file data line by line into an std::vector of std::strings (starting on line 34). Then I used a Pseudo Random Number Generator I found with a quick google search (starting on line 7) to get a random number within the limits of the vector of words (line 64) to pick the word to be scrambled (line 67).
I hope I've been of help.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
unsigned int RandomInt(unsigned int min, unsigned int max)
{
return (rand() % ((max + 1) - min) + min);
}
int main()
{
// #Step One# - Open File
//===========================================
std::string wordFile = "./wordList.txt" ;
std::ifstream fileIn(wordFile.c_str());
// Check if the file was opened sucessfully
if (!fileIn)
{
std::cerr << "err: Failed To Load (" << wordFile << ")\n" ;
return EXIT_FAILURE;
}
// #Step Two# - Read Words From File
// (Assuming each word is on its own line.)
//===========================================
std::vector<std::string> words;
std::string line;
while (getline(fileIn, line))
{
words.push_back(line);
line.clear();
}
fileIn.close();
// Check if the words list was populated
if (words.empty())
{
std::cerr << "err: No Words Found\n" ;
return EXIT_FAILURE;
}
// #Step Three# - Game Loop
//===========================================
std::cout << "! - CRYPTO GUESS - !\n" ;
// Seed the pseudo random number generator
srand(static_cast <unsigned int >(time(NULL)));
for (bool quitGame = false ; quitGame == false ;)
{
// %Part One% - Pick A Random Word
//-------------------------------------
// Generate Random Number
unsigned int randomNumber = RandomInt(0, words.size() - 1);
// Set the word
std::string word = words[randomNumber];
// %Part Two% - Shuffle the Word
//-------------------------------------
std::string shuffledWord = word;
std::random_shuffle(shuffledWord.begin(), shuffledWord.end());
// %Part Three% - Guess The Word
//-------------------------------------
std::cout << "\n[Shuffled Word]: " << shuffledWord
<< " (Type q to Quit)\n" << "Your guess...\n" ;
for (bool done = false ; done == false ;)
{
std::string answer;
std::cin >> answer;
if (answer == word)
{
std::cout << "Is Correct!\n" ;
done = true ;
}
else if ((answer == "q" ) || (answer == "Q" ))
{
done = true ;
quitGame = true ;
}
else
std::cout << "Is Incorrect...Try Again!\n" ;
}
}
// #Step Four# - Exit
//===========================================
std::cout << "Thanks for Playing!\n" ;
return EXIT_SUCCESS;
}
Jun 5, 2013 at 4:50am Jun 5, 2013 at 4:50am UTC
Topic archived. No new replies allowed.