How to pick a random word from a user entered .txt file (HANGMAN GAME)

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:

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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include <iostream>
#include <string>
#include<fstream>
#include<stdio.h>
#include<string.h>
#include<time.h>
 
//#define MAX_STRING_SIZE 1000
using namespace 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++;
				}
			}
			else if (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;

}
Last edited on
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
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#include <iostream>
#include <string>
#include<fstream>
#include<stdio.h>
#include<string.h>
#include<ctime>
#include<cstdlib>
using namespace 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];
cout<<hangmanArray[i]<<endl;
}
}else{
cout<<endl<<"I'm Sorry. File "<<filename<<" could not be Opened."<<endl;
return 1;
}

int RandIndex = rand() % 20;
hangmanArray[RandIndex] = randomword;

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++;
}
}
else if (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;
}
Last edited on
I refuse to read any of that.

But to answer the question:

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.

Help with random numbers:
http://www.cplusplus.com/faq/beginners/random-numbers/
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 ran the program, and it did not seem to even pick a word at all, it started the game with no word at all.

what comes up when I compile:

Enter the name of the file that you wish to use:
words.txt

(returns all of the words in the .txt file)

There are 0 letters with spaces
You have 6 tries left in this game
Winner!
Guess a letter

did the for loop not work in copying all of the .txt file into the array, or is something else going wrong?
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:

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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#include <iostream>
#include <string>
#include<fstream>
#include<stdio.h>
#include<string.h>
#include<ctime>
#include<cstdlib>
#include<stdlib.h>
#include<time.h>
using namespace std;

int random_int_in_range(int first, int last) //function that generates a random number in between a certain range
{
	
	unsigned int N = (last - first <= RAND_MAX)  
		? (last - first + 1U)       
		: (RAND_MAX + 1U);            
	unsigned int x = (RAND_MAX + 1U) / N;
	unsigned int y = x * N;
	unsigned int 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++;
				}
			}
			else if (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;
}
where is the file located at? in the same location as the exe? In a different directory?
closed account (48T7M4Gy)
Hello rayruth292

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 :)
Topic archived. No new replies allowed.