My real Hangman help Post

Pages: 12
Ive got
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
#include <cstdlib>
#include <iostream>
#include <string>

using namespace std;

int main()
{
   string words[]={cat, dog, mom, dad, grandma, grandpa, everything, elephant} ;
   srand((unsigned)time(0)) ;
   int pickword=rand()%8+1 ;
   switch (pickword)
   {
      case 1:
         int guessnumber ;
         cout<<"______" <<endl ;
         cout<<"|         |" <<endl ;
         cout<<"|" <<endl ;
         cout<<"|" <<endl ;
         cout<<"|" <<endl ;
         cout<<"|" <<endl ;
         cout<<"|" <<endl ;
         cout<<"***" <<endl ;
         char letter ;
         cout<<"Guess a letter." <<endl ;
         cin>> letter ;

Now what should I do?
Wait, for the random int part make that
 
int pickword=rand()%8 ;

I've got a few mistakes I can fix, so now here's the code I've got so far:
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
#include <cstdlib>
#include <iostream>
#include <string>

using namespace std;

int main()
{
   string words[]={cat, dog, mom, dad, grandma, grandpa, everything, elephant} ;
   srand((unsigned)time(0)) ;
   int pickword=rand()%8 ;
   switch (pickword)
   {
      case 1:
         int guessnumber ;
         cout<<"______" <<endl ;
         cout<<"|    |" <<endl ;
         cout<<"|" <<endl ;
         cout<<"|" <<endl ;
         cout<<"|" <<endl ;
         cout<<"|" <<endl ;
         cout<<"|" <<endl ;
         cout<<"***" <<endl ;
         char letter ;
         cout<<"Guess a letter." <<endl ;
         cin>> letter ;

NOW FOR REAL, what do I do?
Last edited on

- Line 9 will not compile. why do you think is the reason?
- why are Lines 15-26 inside the switch-case? wouldn't it be better in a separate function?
- in fact, there is no need for a switch case:
const string& wordpicked = words[pickword];

(also, if you want to be helped, please avoid multi-posting)
Last edited on
Would I add
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
if (letter=c)
{
   cout<<"c**" <<endl ;
   goto guess2 ;
}
if  (letter=a)
{
   cout<<"*a*" <<endl ;
   goto guess3 ;
}
if (letter=t)
{
   cout<<"**t" <<endl ;
   goto guess4 ;
}
else
{
   guessnumber++ ;
}
guess2:
cin>> letter ;
if (letter=a)
{
   cout<<"ca*" <<endl ;
   goto guess5 ;
}
if (letter=t)
{
   cout<<"c*t" <<endl ;
   goto guess6 ;
}
else
{
   guessnumber++ ;
}
guess3:
cin>> letter ;
if (letter=c)
{
   cout<<"ca*" <<endl ;
   goto guess5 ;
}
if (letter=t)
{
   cout<<"*at" <<endl ;
   goto guess7 ;
}
else
{
   guessnumber++ ;
}
guess4:
cin>> letter ;
if (letter=c)
{
   cout<<"c*t" <<endl ;
   goto guess6 ;
}
if (letter=a)
{
   cout<<"*at" <<endl ;
   goto guess7 ;
}
else
{
   guessnumber++ ;
}

Then I'lll have to add some stuff and I might have it. I just don't feel like adding it right now.
Last edited on
So now in total I've got:
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
#include <cstdlib>
#include <iostream>
#include <string>

using namespace std;

int main()
{
   string words[]={cat, dog, mom, dad} ;
   srand((unsigned)time(0)) ;
   int pickword=rand()%4 ;
   switch (pickword)
   {
      case 1:
         int guessnumber ;
         cout<<"______" <<endl ;
         cout<<"|    |" <<endl ;
         cout<<"|" <<endl ;
         cout<<"|" <<endl ;
         cout<<"|" <<endl ;
         cout<<"|" <<endl ;
         cout<<"|" <<endl ;
         cout<<"***" <<endl ;
         char letter ;
         cout<<"Guess a letter." <<endl ;
         cin>> letter ;
         if (letter=c)
         {
            cout<<"c**" <<endl ;
            goto guess2 ;
         }
         if  (letter=a)
         {
            cout<<"*a*" <<endl ;
            goto guess3 ;
         }
         if (letter=t)
         {
            cout<<"**t" <<endl ;
            goto guess4 ;
         }
         else
         {
            guessnumber++ ;
         }
         guess2:
         cin>> letter ;
         if (letter=a)
         {
            cout<<"ca*" <<endl ;
            goto guess5 ;
         }
         if (letter=t)
         {
            cout<<"c*t" <<endl ;
            goto guess6 ;
         }
         else
         {
            guessnumber++ ;
            goto guess2 ;
         }
         guess3:
         cin>> letter ;
         if (letter=c)
         {
            cout<<"ca*" <<endl ;
            goto guess5 ;
         }
         if (letter=t)
         {
            cout<<"*at" <<endl ;
            goto guess7 ;
         }
         else
         {
            guessnumber++ ;
            goto guess3 ;
         }
         guess4:
         cin>> letter ;
         if (letter=c)
         {
            cout<<"c*t" <<endl ;
            goto guess6 ;
         }
         if (letter=a)
         {
            cout<<"*at" <<endl ;
            goto guess7 ;
         }
         else
         {
            guessnumber++ ;
            goto guess4 ;
         }
         guess5:
         cin>> letter ;
         if (letter=t)
         {
            cout<<"cat     You win!" <<endl ;
            break ;
         }
         else
         {
            guessnumber++ ;
            goto guess5 ;
         }
         guess6:
         cin>> letter ;
         if (letter=a)
         {
            cout<<"cat     You win!" <<endl ;
            break ;
         }
         else
         {
            guessnumber++ ;
            goto guess6 ;
         }
         guess7:
         cin>> letter ;
         if (letter=t)
         {
            cout<<"cat     You win!" <<endl ;
            break ;
         }
         else
         {
            guessnumber++ ;
            goto guess7 ;
         }
         break ;
      default:
         cout<<"This word is unknown." <<endl ;
   }

Thats for the word cat. All I have to do is add the steps for hanging the man, copy and paste in every else for each guess and I have finished for the word cat.
Last edited on
- in C++, = is assignment, == is comparison
- your 67 lines above is not a good approach - you will have to write crazy amounts of code every time someone adds a word

look at this thread for string arrays:
http://www.cplusplus.com/forum/beginner/42240/

suggestion:
1. write a new small piece of code
2. make sure that piece of code compiles
3. if it does what you expect, goto 1
4. else debug until that new small piece of code works

this type of procedure is called "unit testing" and will give you a better chance of writing something that compiles and works
Last edited on
Could you seriously help me. I'm going crazy I can't think straight and I dont know what
 
const string& wordpicked=words[pickword]

is.
PLEASE SERIOUSLY HELP ME!! THANKS!
Could I like start over and maybe you could give me hints/tips. That would be a big help.
Heres the first part, I will probably stick with:
1
2
3
4
5
6
7
8
9
10
11
12
#include <cstdlib>
#include <iostream>
#include <string>

using namespace std;

int main()
{
   string words[]={"cat", "dog", "mom", "dad", "grandma", "grandpa", "everything"} ;
   srand((unsigned)time(0)) ;
   int pickword=rand()%7 ;
   const string& wordpicked=words[pickword] ;

Now whats the best idea for the next step?
now you have a while() loop prompting the user for characters
there are two ways to break out of the while() loop: winning and losing (what are the conditions?)
test for these conditions inside the while until you either win or lose

that's all
could you give me a code theres one thing pestering me about it. How do you keep the c for ex. there when you guess a new letter. Ugh.
in a char if you only need to keep one
in a char[] if you need to keep more than one
I dont get it srry. Sometimes I don't understand things when I should, even though I'm really smart. Its annoying, and makes you feel stupid. Please help and explain.
Well put it this way. I get the part where it asks for a character to spell the word, but how to say if you get all the letters you win. How do you say "I got all the letters I need to put them together and make the word and I win" Just basically help. I'm probably confusing you by now.
huh? could i get some kind of code for it. I understand the codes better when I see how I should have set it up but I couldn't think of it that way.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <cstdlib>
#include <iostream>
#include <string>

using namespace std;

int main()
{
   string words[]={"cat", "dog", "mom", "dad", "grandma", "grandpa", "everything"} ;
   srand((unsigned)time(0)) ;
   int pickword=rand()%7 ;
   const string& wordpicked=words[pickword] ;
   while ()          //what goes in the parentheses for the while loop???
   {
      char[]=         //what does char[]=???
                     //what goes here??? 

What?
Last edited on
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
#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;

void display()
{
         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()
{
        srand((unsigned)time(0)) ;
        int pickword=rand()%8 ;
        pickedword = words[pickword]
        guessword.assign("*", pickedword.length());

}

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
                     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
                     break;
             }// switch of userchoice
      } // while user choice

      return 0
} // end of main
   


this is the basics of what you are trying to do. I don't know if you will understand it or not.
Last edited on
I understand what it does but one question.
What does
 
guessword.assign("*", pickedword.length()) ;

mean? That's all and thanks. I will make sure I will remember some of this when creating other games.
And I need a little help with the switch case. What should go there? Its 6:56 AM here so give me a break . Thanks. I understand what im trying to do, but i can't seem to put it into c++ format.
Pages: 12