Novice programmer needing some help

So here's my story. I'm not in school and not in the programming field. But I'm a novice programmer who just started working on C++. My background is Basic, QBasic to be exact. I decided to work on a "Text Only" version of Hangman, like the ones you'd find on old disks and whatnot. Just the basics right. So I'm wanting to add the entire English Dictionary into the game but Code::Blocks 17.12 keeps crashing if I add to many words into the Word Bank so I was trying to figure out how to put said words into a separate text file to prevent crashing and so that I can add every word in the English Dictionary. If that makes any sense? All help would be appreciated. I'm an extremely slow learner so I've been trying to do this for a few days but gave up. Please don't judge.

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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
  //GNU General Public License v3.0
//The Gallows - Version 1.0 - December 31st, 2019 - DBT
//More Additions to come in due time.

#include <ctime>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <windows.h>
#include <fstream>
#include <stdlib.h>
#include <time.h>
#include <stdexcept>

void Color(uint8_t, uint8_t);
void ClearConsoleToEndOfLine();
void ClearScreen();
void EnhanceHiddenMessage(std::string);

const char BlankFiller = '_';

namespace Gallows
{
    char ConsoleTitle[] = "The Gallows - Ver 1.0";
    COORD CursorCoords = {0, 0};
    COORD NewScreenBufferDimensions = {81, 6000}; // Console Buffer Width, Height
    SMALL_RECT NewsrWindow = {0, 0, 81, 40};      // New Console Window StartX, StartY, EndX, EndY
    COORD MaximumWindowSize = {81, 40};           // Console Window Maximum Width, Height

    CONST UINT8 Palette1 = 0;
    UINT8 CurrentColorPalette = 1;

    UINT8 PrimaryWinColor[] = {0x2E, 0xF0, 0xF0};
    UINT8 CurrentWinColor;

    COLORREF ColorTable[1][16] =
    {
        RGB(   0,  16,   0),
        RGB(   0,  32,   0),
        RGB(   0,  48,   0),
        RGB(   0,  64,   0),
        RGB(   0,  80,   0),
        RGB(   0,  96,   0),
        RGB(   0, 112,   0),
        RGB(   0, 128,   0),
        RGB(   0, 144,   0),
        RGB(   0, 160,   0),
        RGB(   0, 176,   0),
        RGB(   0, 192,   0),
        RGB(   0, 208,   0),
        RGB(   0, 224,   0),
        RGB(   0, 240,   0),
        RGB( 255, 255, 255),

    };

}

std::string randomWord(){
std::string words[] = {

//////////////////Word Bank

//A

"aaron",
"abandoned",
"aberdeen",
"abilities",
"ability",
"able",
"aboriginal",
"abortion",
"about",

	};

	return words[rand() % (*(&words + 1) - words)];
}

int letterFill(char guess, std::string secretword, std::string &guessword){

    int matches=0;

    for (int i = 0; i < (int)secretword.length(); i++)
    {

        if (guess == guessword[i])
        {
			matches--;
		} else if (guess == secretword[i]) {
            guessword[i] = guess;
            matches++;
        }
    }
    return matches;
}

int main(){                                                             //Start

	srand (time(NULL));

	const int Win{1};
	const int StillPlaying{0};
	const int Lose{-1};

	int tries;                                                       //Player tries
	int WinOrLose;
	int LetterMatches;
	bool Replay{true};

	char letter;                                                  //Letter Input
	std::string word;
	std::string HiddenMessage;

	do
	{
		tries = 6;
		WinOrLose = StillPlaying;
		word = randomWord();

		HiddenMessage.clear();
		HiddenMessage.resize (word.length(), BlankFiller);

		transform(word.begin(), word.end(), word.begin(), ::toupper);

        Color(0, 2);

        std::cout << "===============================The Gallows Ver. 1.0===================================\n";
		std::cout << "/   December 31st 1877                                                                \n";
		std::cout << "/                                                                                     \n";
		std::cout << "/   You have been tried for Cattle theft and Murder. It's the day of your execution   \n";               //Intro Line
		std::cout << "/   and you're hoping for a miracle...anything to get you out of being hanged. You    \n";
		std::cout << "/   never thought for a moment about pulling the trigger but here you are. Now your   \n";
		std::cout << "/   here. Suddenly, a cloaked figure offers you one last chance for freedom and save  \n";
		std::cout << "/   yourself from the hangman's noose.                                                \n";
		std::cout << "/                                                                                     \n";
		std::cout << "/   Your word has been chosen. The word is " << word.length() << " letters long!\n";             //Length of the word

		while (WinOrLose == StillPlaying) {

			std::cout << "\n";
			EnhanceHiddenMessage(HiddenMessage);
			std::cout << "\n\n";
			Color(0, 2);

			std::cout << "=Which letter would you like to choose this time? ";
			std::cin >> letter;

			letter = toupper(letter);

			std::cout << "\n";

			LetterMatches = letterFill(letter, word, HiddenMessage);

			if(LetterMatches == 0)
			{
				std::cout << "=Could not find " << letter << " in the word.\n\n";
				tries--;

				std::cout << "=Guess a letter. You have " << tries << " attempt";
				if(tries>1) std::cout << "s";
				std::cout << " left.\n\n";

			} else if(LetterMatches > 0) {

				std::cout << "=Yes!!! There ";

				if(LetterMatches == 1)
				{
					std::cout << "is ";
				} else {
					std::cout << "are ";
				}

				std::cout << LetterMatches << " " << letter;

				if(LetterMatches > 1) std::cout << "'s";

				std::cout << " in your life saving word!\n\n";
			} else if(LetterMatches < 0) {
				std::cout << "=You have already guessed the letter " << letter << ".\n=Please try again!\n\n";
			}

			if (word == HiddenMessage) WinOrLose = Win;
			if (tries < 1) WinOrLose = Lose;
		}

		if(WinOrLose == Win)
		{
			std::cout << "\n\n=Huzzah! You win Walter!\n\n";     //When player wins
			EnhanceHiddenMessage(word);
			Color(0, 2);
			std::cout << "Congratulations, you live to see another day...\n=It means you are free to leave the gallows. Don't come back!\n";
		} else {
			std::cout << "\n\n=Sorry, Walter! You lost and were hanged...pity\n";       //When player dies
			std::cout << "=The word to your freedom was: ";
			EnhanceHiddenMessage(word);
			Color(0,2);
			std::cout << "\n";
		}

		do
		{
			std::cout << "\n\n=Would you like to play again (Y / N)?: ";
			std::cin >> letter;

			letter = toupper(letter);

			if(letter == 'N') Replay = false;

		} while ((letter != 'Y') && (letter != 'N'));

		std::cout << "\n\n\n";

	} while(Replay == true);

	std::cout << "=Thank you for playing!\n=Don't come back now you hear!!!\n\n";

	return 0;
}

void Color(uint8_t ForegroundColor, uint8_t BackgroundColor)
{
    HANDLE hstdout = GetStdHandle( STD_OUTPUT_HANDLE );

    SetConsoleTextAttribute(hstdout, ((ForegroundColor & 0x0F) << 4) | (BackgroundColor & 0x0F));
}

void EnhanceHiddenMessage(std::string HiddenMessage)
{

    for(unsigned int Iterator1 = 0; Iterator1 < HiddenMessage.length(); Iterator1++)
    {
        HiddenMessage[Iterator1] == BlankFiller ? Color(0, 2) : Color(0, 10);
        std::cout << HiddenMessage[Iterator1];
    }

    std::cout <<"\n";
}
Now all you need is a word list. There are lot's around if you google 'word list' and you can cut and paste combine them and process to remove duplicates etc.

Free 20-40k word lists are pretty much easy to get hold of.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// READ WORDS FROM FILE LIST
    int count = 0;
    std::ifstream myfile ("words.txt");
    if (myfile.is_open())
    {
        while ( myfile >> word_from_file)
        {
                count++;
                std::cout 
                << count << ' ' << word_from_file.length() 
                << ' ' <<    word_from_file;

                std::cout << '\n';
                    
            }
        }
        
        myfile.close();
    }
    
    else
        std::cout << "Unable to open file";
Change words to a vector and read them from a file.
1
2
3
4
5
6
vector<string> words;
ifstream wordStream("words.txt");
string dummy;
while(wordStream >> dummy) {
    words.push_back(dummy);
}

<vector>? Maybe but it depends what the process is. There are no hard and fast rules. In my case it is a cut and paste from something else where the words are processed for a puzzle, but not stored.
I was just wondering where I need to post this code? I know it's in the Main section. But I keep getting errors.
<vector>? Maybe but it depends what the process is.
I admit it's far from ideal, but for a beginner I think it's okay.

For hangman, you don't need to keep all the words in RAM, you just need to select one at random. So a better method might be:
- preprocess the word list by counting the words. Then edit the file and put the number of words on the first line.
- Have the program read the number of words (N) from the file. Then select a random number from 1 to N and pick the N'th word.
Topic archived. No new replies allowed.