I need to pick a random word from a user entered .txt file, and use that word in my hangman game. I can get it to work if I manually enter a word, but cannot for the life of me get it to pick a random word from the user entered .txt file. Any help is greatly appreciated. My source code is as follows:
#include <iostream>
#include <string>
#include<fstream>
#include<stdio.h>
#include<string.h>
#include<time.h>
//#define MAX_STRING_SIZE 1000
usingnamespace std;
struct randomWords
{
string randomword;
};
int readData(randomWords words[]);
int main()
{
string hangmanArray[20] = {};
srand(time(NULL));
ifstream fin;
char ans = 'n';
string filename = "";
string randomword;
bool keepRunning = true;
while (keepRunning == true)
{
cout << "Enter the name of the file you wish to use: " << endl;
cin >> filename;
ifstream file(filename);
if (file.is_open)
{
string hangmanArray[20];
for (int i = 0; i < 20; i++)
{
file >> hangmanArray[i];
}
}
int RandIndex = rand() % 20;
hangmanArray[RandIndex] = randomword;
//cout << "Enter the word you wish to use " << endl;
//cin >> filename;
//fin.open(filename)
string randomword;
getline(cin, randomword);
string copy = randomword;
string underline;
for (int i = 0; i != randomword.length(); i++){
if (randomword.at(i) == ' '){
underline += " ";
}
else
{
underline += "_";
}
}
for (int i = 0; i != 50; ++i){
cout << endl;
}
string guess;
int wrong = 0;
while (1){
if (wrong == 6){
cout << "You ran out of tries. The word was: " << randomword << endl;
break;
}
cout << underline << endl;
cout << "There are " << randomword.length() << " letters with spaces" << endl;
cout << "You have " << 6 - wrong << " tries remaining in this game" << endl;
if (underline == randomword){
cout << "Winner!" << endl;
}
cout << "Guess a letter" << endl;
getline(cin, guess);
if (guess.length() > 1){
if (guess == randomword){
cout << "Winner!" << endl;
break;
}
else{
cout << "Word is not correct " << endl;
wrong++;
}
}
elseif (copy.find(guess) != -1){
while (copy.find(guess) != -1){
underline.replace(copy.find(guess), 1, guess);
copy.replace(copy.find(guess), 1, "_");
}
}
else
{
cout << "Not correct " << endl;
wrong++;
}
cout << endl;
}
cout << "\nWould you like to try again?" << "\nEnter Y or y to continue.";
cin >> ans;
if (ans != 'Y') // these series of if statements will determine whether or not the user wants to continue testing passwords
{
if (ans != 'y')
{
keepRunning = false; //if the answer is equal to Y or y, the user does want to test other passwords and if not, the program will close
}
}
}
cin.get();
cin.get();
return 0;
Please use code Tags! They Help us to read your code! http://www.cplusplus.com/articles/jEywvCM9/
I don't think you fully understand the use of break;
I made an edit to test if the file is actually opening here:
1 - Load all of your words into memory (into an array or vector)
2 - Choose a random number between 0 (inclusive) and the number of words you have (exclusive)
3 - That is the index of the word in the array/vector you should use.
Thank you for the help, Jason. It does seem to be opening the file, but it does not seem to be taking a word from a .txt file, and implementing it into the game.
Duoas, I thought I had loaded all of my words into an array, using the for loop? What would be the next step in picking a random word from this array to load into the program?
What I noticed is that it is opening the file and seeing into it but it's not seeing the whole file. I tested with a file with three words in it but it only saw the first two words.
I changed the random number generator up a little bit, and I checked, it is returning a random number, but why won't the array index transfer into string random word?
Source code is here:
#include <iostream>
#include <string>
#include<fstream>
#include<stdio.h>
#include<string.h>
#include<ctime>
#include<cstdlib>
#include<stdlib.h>
#include<time.h>
usingnamespace std;
int random_int_in_range(int first, int last) //function that generates a random number in between a certain range
{
unsignedint N = (last - first <= RAND_MAX)
? (last - first + 1U)
: (RAND_MAX + 1U);
unsignedint x = (RAND_MAX + 1U) / N;
unsignedint y = x * N;
unsignedint r;
do {
r = rand();
} while (r >= y);
return r / x + first;
}
struct randomWords
{
string randomword;
};
int readData(randomWords words[]);
int main()
{
string hangmanArray[20] = {};
srand(time(NULL));
ifstream fin;
char ans = 'n';
string filename = "";
string randomword;
bool keepRunning = true;
while (keepRunning == true)
{
cout << "Enter the name of the file you wish to use: " << endl;
cin >> filename;
ifstream file(filename);
if (file.is_open())
{
string hangmanArray[20];
for (int i = 0; i < 20; i++)
{
file >> hangmanArray[i];
//cout << hangmanArray[i] << endl; <-- was a test to see if the file was passing to the array
}
}
else{
cout << endl << "I'm Sorry. File " << filename << " could not be Opened." << endl;
return 1;
}
int RandIndex = random_int_in_range(1, 20);
cout << RandIndex; //was a test to see if a random number was bein generated
hangmanArray[RandIndex] = randomword;
getline(cin, randomword);
string copy = randomword;
string underline;
for (int i = 0; i != randomword.length(); i++){
if (randomword.at(i) == ' '){
underline += " ";
}
else
{
underline += "_";
}
}
for (int i = 0; i != 50; ++i){
cout << endl;
}
string guess;
int wrong = 0;
while (1){
if (wrong == 6){
cout << "You ran out of tries. The word was: " << randomword << endl;
break;
}
cout << underline << endl;
cout << "There are " << randomword.length() << " letters with spaces" << endl;
cout << "You have " << 6 - wrong << " tries remaining in this game" << endl;
if (underline == randomword){
cout << "Winner!" << endl;
}
cout << "Guess a letter" << endl;
getline(cin, guess);
if (guess.length() > 1){
if (guess == randomword){
cout << "Winner!" << endl;
break;
}
else{
cout << "Word is not correct " << endl;
wrong++;
}
}
elseif (copy.find(guess) != -1){
while (copy.find(guess) != -1){
underline.replace(copy.find(guess), 1, guess);
copy.replace(copy.find(guess), 1, "_");
}
}
else
{
cout << "Not correct " << endl;
wrong++;
}
cout << endl;
}
cout << "\nWould you like to try again?" << "\nEnter Y or y to continue.";
cin >> ans;
if (ans != 'Y' || ans != 'y')
{
keepRunning = false;
}
}
cin.get();
return 0;
}
I'm sure you want to solve your problem but why don't you take the advice from Duoas. You'll find it quicker and easier to solve issues if you break them down into parts instead of going backwards and forwards each time with 143 lines of code.
It seems your first step is one of two possibilities
EITHER
- write a small program to just read a word file and print out the words (and as jason writes the txt file and exe file must be together unless you're going to get bogged down further with directories.
OR
- make up a hard coded array/vector of 5 words and get random words from that
Feel free to take whichever path you like but the path you're on is not a productive one almost at spinning wheels stage :)