My real Hangman help Post

Pages: 12
The main part is telling whether or not the letters make the word I can't just say
1
2
3
4
if (letter1+letter2+letter3=words[0])  //words[0] is "cat"
{
   cout<<"You win!" <<endl ;
}
Last edited on
This is what I tried for the letters part and got an error:
1
2
3
4
5
6
7
8
9
10
11
cout<<"Guess some letters" <<endl ;
int letter, letter2, letter3 ; //I was going to get it to make the word cat at first even if i had to restart the program
cin>> letter ;
cin>> letter2 ;
cin>> letter3 ;
char word[]={letter, letter2, letter3} ;
if (word[]=words[pickword]) //this is where i got the error 
{
   cout<<"You win!" <<endl ;
}
break ;

Help Please! Thanks.
Last edited on
So I under stand how to make it.
I would guess a letter and if its correct i would
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
char guess ;
cout<<"Guess a letter." <<endl ;
guess:
cin>> guess ;
words[0]={"c", "a", "t"} ;
switch (guess)
{
   case 'c':
      display(words[0][0]) ;
      goto guess ;
   case 'a':
      display(words[0][1]) ;
      goto guess ;
   case 't':
      display(words[0][2]) ;
      goto guess ;
}

Then I would add the guessing the word part and the "hanging the man" steps, pretty much, correct? And if there's problems with this (I know it's a little long and i'd have to do that for every word, but thats ok for now) could you tweak them and explain. Thank you for all the help. Really appreciate it!
Last edited on
And for the hanging the man part:
1
2
3
4
5
6
7
8
9
10
11
12
case 't:' //this is to show where im at
//dot dot dot
//to the man
default:
   guessnumber++ ;
   if (guessnumber==1)
   {
      cout<<"______" <<endl ;
      cout<<"|     |" <<endl ;
      cout<<"|     0" <<endl ;
      cout<<"|" <<endl ;
//dot dot dot until i am finished 
I guess you didn't understand what I put up. I put up the entire thought process for hangman with out completing the entire code.

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
#include <iostream>
#include <string>
#include <cstdlib> // guess I need it for rand stuff


using namespace std;
string words[]={"cat", "dog", "mom", "dad", "grandma", "grandpa", "everything",                           "elephant"} ;
string pickedword;
string guessword;
int guesscount = 0;

void display()
{

         // this is info I want to display more than once
         // for example like after each time I guess something.
         cout<<"______" <<endl ;
         cout<<"|    |" <<endl ;
         cout<<"|" <<endl ;
         cout<<"|" <<endl ;
         cout<<"|" <<endl ;
         cout<<"|" <<endl ;
         cout<<"|" <<endl ;
         cout<<"***" <<endl ;
         cout<< endl;
         cout<< guessword << endl;
         cout<< endl;
         cout<< "1- guess a letter" << endl;
         cout<< "2- guess a word" << endl;
         cout<< "3- give up and quit" << endl;
}

void SetupWord()
{
        // basically I want to set up my two strings, which is the picked word
        // then I want another string of the same length with a neutral character.
        srand((unsigned)time(0)) ;
        int pickword=rand()%8 ;
        pickedword = words[pickword]
        
        // fill an alternate string with a neutral character with the length of the picked word.
        // look up string class for for more information on assign.
        guessword.assign("*", pickedword.length());

        // initialize guess count for every time we set up a word.
        guesscount =0;
}

void guessAletter()
{
       char inChar;  // the character I read in.
       bool Ifound = false;
       int count = 0;
       // user guesses a letter
       cout << "Guess A letter: ";
       cin >> inChar;
       // I making a guess, add one to the guess count.
       guesscount++;
       int index = 0
       // loop through the pickedword's string to find each occurrence  
       for(index = 0; index < pickedword.length(); index++)
       {
               if(pickedword[index] == inChar)
               {
                       IFound = true; // we found something, will use later...
                       // in the guess word, which is the same length of pickedword
                       // I replace the neutral character with the guessed letter.
                       guessword[index] = inChar;  
                       count++;  // I just want to know how many times I find it.

               }
       }

       if(Ifound)  // this is whether I find something or not.
       {
              // report that we found something....
              cout << "The Letter " << inChar << " was found " << count << " times." << endl;
              // test if guessword is equal to pickedword.
              if(guessword == pickedword)
              {
                      // report on success
                      cout << "You have picked all the letters in " << guessword << " in " << 
                           guesscount << " tries." << endl;'
                     
                      // set up a new word for another go around.
                      SetupWord();
               
               }
        }
        else
        { 
                  // I didn't find that letter
                  cout << "I am sorry  " << inChar << " was not found."  << endl;
                 // this is where I would update my graphics for the hangman....
        }
}

void guessAWord()
{
        string usrguess;
        guesscount++;
        // get the user guess for a word
        cout << "Guess a Word: "
        cin >> usrguess;
      
        // see if they match.
        if(usrguess == pickedword)
        {
                cout << "You have guessed the word in " << guesscount << " Tries.";
                // set up a new word....
                SetupWord()
        }
        else
        {
                cout << "I am sorry " << usrguess << " was not the word."  << endl;
                // this is where I would update my graphic for the hangman stuff...

        }
}

int main()
{

   int guessnumber = 0;
   int userchoice = 0;
   // set up the first word
   SetupWord();

   while(userchoice  < 3)
   {
         display();
         cin >> userchoice;
         switch(userchoice)
         {
               case 1:
                      //pick a letter
                      // basic algorithm...
                      // use a cin to get a char;
                      // loop against the picked word until I find all occurrences
                      // at each found letter use guessword[] with the found position and replace with char.  
                     // increment the guess count.
                     // if guessword == pickedword we are successful at completing the word in x guesses.
                     // else we let another round of the loop go by
                     guessALetter();
                     break;
               case 2:
                     // guess a word
                     // cin to get the word guess
                     // if usrguessword == pickedword we win in x guesses and we set up for another run
                     // else we increment guess count, sorry try again let the loop go around again
                     guessAWord();
                     break;
             }// switch of userchoice
      } // while user choice

      return 0
} // end of main 


You need to look at your full design of what your code is suppose to do and figure out each step. If I wanted to add graphics to it I would change the design of display slightly, or break it in to two parts where I could manage the graphics.

This version doesn't have the graphic updates in it I would have to think about that a little bit but this version doesn't care which word is picked in the list, and uses it for finding the matches.
Last edited on
I am sorry it shows 3 letters for each word, does not show correct letters when inputted, (doesn't show any letters), and does not hang the man. This is not exactly what I wanted.
Thanks for all your help. Got me on the right track and I need to build my program, but I will change a bit and it should work. Thanks for trying to help. Taught me a few things as well. Appreciate it!
This is for me to Copy:
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
#include <cstdlib>
#include <iostream>
#include <string>

using namespace std;

string words[]={"cat", "dog", "mom", "dad", "grandma,", "grandpa", "everything"} ;
int numlet[]={3, 3, 3, 3, 7, 7, 10} ;
string pickedword ;
void display()
{
     cout<<"______" <<endl ;
     cout<<"|    |" <<endl ;
     cout<<"|" <<endl ;
     cout<<"|" <<endl ;
     cout<<"|" <<endl ;
     cout<<"|" <<endl ;
     cout<<"|" <<endl ;
     srand((unsigned)time(0)) ;
     int pickword=rand()%7 ;
     pickedword=words[pickword] ;
     int letters ;
     for (letters=0; letters<numlet[pickword]; letters++)
     {
         cout<<"*" ;
     }
}
void hmS1() 
{
     cout<<"______" <<endl ;
     cout<<"|    |" <<endl ;
     cout<<"|    0" <<endl ;
     cout<<"|" <<endl ;
     cout<<"|" <<endl ;
     cout<<"|" <<endl ;
     cout<<"|" <<endl ;
}
void hmS2()
{
     cout<<"______" <<endl ;
     cout<<"|    |" <<endl ;
     cout<<"|    0" <<endl ;
     cout<<"|   /" <<endl ;
     cout<<"|" <<endl ;
     cout<<"|" <<endl ;
     cout<<"|" <<endl ;
}
void hmS3()
{
     cout<<"______" <<endl ;
     cout<<"|    |" <<endl ;
     cout<<"|    0" <<endl ;
     cout<<"|   / \ " <<endl ;
     cout<<"|" <<endl ;
     cout<<"|" <<endl ;
     cout<<"|" <<endl ;
}
void hmS4()
{
     cout<<"______" <<endl ;
     cout<<"|    |" <<endl ;
     cout<<"|    0" <<endl ;
     cout<<"|   /8\ " <<endl ;
     cout<<"|" <<endl ;
     cout<<"|" <<endl ;
     cout<<"|" <<endl ;
}
void hmS5()
{
     cout<<"______" <<endl ;
     cout<<"|    |" <<endl ;
     cout<<"|    0" <<endl ;
     cout<<"|   /8\ " <<endl ;
     cout<<"|    8" <<endl ;
     cout<<"|" <<endl ;
     cout<<"|" <<endl ;
}
void hmS6()
{
     cout<<"______" <<endl ;
     cout<<"|    |" <<endl ;
     cout<<"|    0" <<endl ;
     cout<<"|   /8\ " <<endl ;
     cout<<"|    8" <<endl ;
     cout<<"|   /" <<endl ;
     cout<<"|" <<endl ;
}
void hmS7()
{
     cout<<"______" <<endl ;
     cout<<"|    |" <<endl ;
     cout<<"|    0" <<endl ;
     cout<<"|   /8\ " <<endl ;
     cout<<"|    8" <<endl ;
     cout<<"|   / \ " <<endl ;
     cout<<"|" <<endl ;
     cout<<"You lose." <<endl ;
}
int main()
{
    char guess ;
    int guessnumber=0 ;
    display() ;
    switch (words[pickword])
    {
           case words[0]:
                char cat[]={"c", "a", "t"} ;
                bool c, a, t ;
                cout<<"Guess a letter." <<endl ;
                guess:
                if ((c=true)&&(a=true)&&(t=true))
                {
                                                 out<<"cat" <<endl ;
                                                 cout<<"You win!" <<endl ;
                                                 goto end;
                }
                cin>> guess ;
                switch (guess)
                {
                       case 'c':
                            c=true ;
                            cout<<"c**" <<endl ;
                            goto guess ;
                       case 'a':
                            a=true ;
                            cout<<"*a*" <<endl ;
                            goto guess ;
                       case 't':
                            t=true ;
                            cout<<"**t" <<endl ;
                            goto guess ;
                       default:
                               guessnumber++ ;
                               switch (guessnumber)
                               {
                                      case 1:
                                           hmS1() ;
                                           goto guess ;
                                      case 2:
                                           hmS2() ;
                                           goto guess ;
                                      case 3:
                                           hmS3() ;
                                           goto guess ;
                                      case 4:
                                           hmS4() ;
                                           goto guess ;
                                      case 5:
                                           hmS5() ;
                                           goto guess ;
                                      case 6:
                                           hmS6() ;
                                           goto guess ;
                                      case 7:
                                           hmS7() ;
                                           goto end ;
                               }
                }
           case word[1]:
                //same as case word[0]
    end:
    system("PAUSE") ;
}
Topic archived. No new replies allowed.
Pages: 12