Help creating hangman

So I have to create a hangman program that randomly chooses a word from a word bank, the user guesses it, and the programs repeats. No physical hangman has to be drawn the program just has to keep track of wrong/right guesses.

This is what I have so far I'm having some trouble figuring out how to add the word bank into the program. Also trying to figure out how to track wrong guesses. Any help is appreciated. Thanks.

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
#include<iostream>

using namespace std;


int main()
{
   int lives=0;
   char letter;
   int wrong;
   int word;


  [Wordbank]


   while (lives <=5)
 
   {
	wrong=0;
	cout<<endl<<"Guess a letter: ";
	cin>>letter;
				
	for(int n=0; n<20; n++) 
	{
	if(letter!=word)
	wrong+1;
	}
	
	if(wrong<1) 
	{
	lives=lives+1;
	cout<<"Sorry please try again."<<endl; 
	}
   }

	   
   if (lives>5) 
   {
   cout<<endl<<"You lose"; 
   }

   else 
   {
   cout<<endl<<"You win!/n"; 
   }

   getchar();
   return 0;
}
Last edited on
[Wordbank] = read random word from file (or if you have to, create an array of words in the program itself)

another hint, if(letter!=word) will not work as (assuming) word stores the entire word (that means word should be declared as string word and not int word) and you are reading a letter
closed account (3qX21hU5)
First let me state im still very new at C++ and programming in general and I might be wrong with these ideas. But im just going to brainstorm with you cause this problem interested me.

For the wordbank im thinking you can use a container like a vector or something to contain the words. Im guessing you only need like 10 different words or so. Just put 10 or more words into a vector and that can be your wordbank.

this is the part im not so sure about, to randomly pull a word out of the bank im thinking you can probably use the random number generater rand(); to pull a random word out of the wordbank vector. Im not sure if rand() works on a vector and am at work so cant try it out atm.

But this could be a way to do it maybe.
Topic archived. No new replies allowed.