C++ Hangman Game

Pages: 12
Jul 27, 2011 at 3:10pm
How do I assign it only once per guess? do I make n=1? This is the code so far... doesn't work


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
#include<iostream>
#include<conio.h>
using namespace std;

int main()
{
   char storeguess[26];
   int newlife=0;
   char letter;
   bool yes;
   char solution[100]={0};
   char newW[100]={0};
   int LENGTH;
  
   cout<<"Write a word: ";

   cin>>solution;
  
   LENGTH=strlen(solution);
    cout<<"you have six lives to guess the unknown word, it has" <<endl<<LENGTH<<endl<< "letters";

   while ( newlife <6 && strcmp(newW, solution) != 0)
   
   {
	yes=0;
	cout<<endl<<"Guess a letter: ";
	cin>>letter;

	for(int n=0; n<26; n++) {   // This is my attempt to store and check the user's guess... 
	storeguess[n]=letter; 
	if(letter==storeguess[n]){
		cout<<"You already guessed that!";} 
	}
	
	
	for(int n=0; n<100; n++) {
	
	if(solution[n] == letter){
		letter=solution[n];
	    newW[n]=solution[n]; 
	    yes=(1);
	cout<<"nice!"<<"You got this letter!" <<endl<<newW<<endl;} 
	}
	if (yes<0){ 
	cout<<"Sorry please try again."<<endl; 
	newlife=(newlife+1);
	cout << "you lost this many lifes: "<<newlife<<endl;
	cout<<"please continue: "<<endl<<newW;
	}...

Last edited on Jul 27, 2011 at 4:07pm
Jul 27, 2011 at 4:08pm
So I am having a lot of trouble with this use of char arrays. I still haven't figured out the user guessing part. But I must also make the code return the word solution in stars (*)!

I must do a char to hold the *'s. Find the number of elements in the word[] Now, create the *'s based on the number of letters above.

Next scan the word[] for the guessed letter. If it's there, set the corresponding letter in the star[] to the guessed letter. I guess this is similar to the guessing part, but I cant figure it out!
Jul 27, 2011 at 6:44pm
bool guess['z'-'a'+1] = {false}; The info stored in each cell is "was the letter guessed yet". Every cell correspond to a letter (so guess[0]->'a', guess[1]->'b', guess[2]->'c', ...)
The initial estate of the array is 'empty' (false, 0)

When a letter arrives you check the guess array in the correspondent position (by instance if the 'p' comes, you check cell 15).
If it is 'occupied' (true, 1), then the letter was guessed previously
If it is 'empty' , then you set it to 'occupied' and continue.

After that you will check if the letter is 'valid' (it is in the solution)
Jul 27, 2011 at 11:52pm
Yea got the code to work! Thanks a lot guys!!
Topic archived. No new replies allowed.
Pages: 12