Hangman game function issue

Pages: 12
This part of the function is where the action happens.
The user has already inputted the word (word) and is now guessing the letter (guess)

The guess should be in the word... and then output the number of times that letter occurs so the user cannot enter it.

However before I get to that I need it to work and this does not work properly.
If the word is "hello" and I enter 'o' it comes up with "no" and is wrong. But o is in the word hello....

thanks guys

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
while(w<input.size())
{
	  crs->setPosition(xcd,ycd);
	  cout << t << ": ";
      cin >> guess;
	  cout << "                  ";
	  for(int i=0;i<input.size();i++)
	  {
		  if(guess==input[i])
		  {
		    if(crs)
			crs->setPosition(xcd-12,ycd);
			cout<<"You got one";
		    w++;
		    if(crs)
			crs->setPosition(xcd,ycd);
		    cout << t << ": ";
		    cin>>guess;
		  }
		  else
		  {
			  w=w;
			  cout << "no";
			  crs->setPosition(xcd,ycd);
		      cout << t << ": ";
		      cin>>guess;
		  }
		}

	}
1
2
3
4
5
6
7
8
if(guess==input[i])
{
  yes;
}
else
{
  no
}


enter word hello. Guess "o".
h != o;
cout << no
e != o
cout << no
l != o
cout << no
l != o
cout << no
o == o
cout << yes

but you don't get to comparison o == o. You enter new letter.
If you enter "o" 5 times in a row, I think you will get yes.
Last edited on
But I dont want that, how can I rectify this?
I would make a function of type bool, and give it word and letter. And if letter was found return true if not return false.

1
2
3
4
5
6
7
8
9
10
11
bool function(string, char) { find letter}

cin >> guess;
if (function(input, guess))
{
  letter was found;
}
else
{
  letter was not found;
}


something like this.
Instead of looping through the word, character by character, why not just use the find function of string?

1
2
3
4
if (word.find(guess) != string::npos)
{ yes }
else
{ no }


http://www.cplusplus.com/reference/string/string/find/
Last edited on
The find function works great... But how can I make it so... say if the word was "hello"

how to make l appear twice so they dont need to enter in l twice to find the word. It needs to work like real hangman...

thanks!
Don't stop to search http://www.cplusplus.com/reference/string/string/find/

if letter was found it will return position of letter, so start to search again from this position until npos is returned.
How do I output the position then? I want this to find the letter and put it on the word like hang man and then the user can guess another letter but not search for the same letter twice or more if it is the word more than once.


guess[npos] doesnt work?

I currently have...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
while(w<input.size())
{
	crs->setPosition(xcd,ycd);
    cout << t << ": ";
    cin >> guess;
	
	if (input.find(guess) != string::npos)
	{ 
	cout << "hello there" << endl;

	w++;
	}
	else
	{ 
	cout << "hello there NO" << endl; 
	w=w;
	}
}
1
2
3
4
5
while(w=input.find(guess) < input.size()){
// If guess was found, then this code will be executed.
}
// Otherwise, w will become npos which Should be greater than input.size().
// The loop will exit. 



1
2
3
while(w=input.find(guess) != string::npos){
// This will also work.
}


(If anyone notices an error, or I am blatantly wrong, please say so. I haven't tested this code, but it looks like it will work to me.)
Last edited on
I dont get it.
What is npos doing.

w is merely counting the correct answers.. When w=string size it means the word has been found, yes?

thanks
um.. no lol. w!=string size. w=input.find(guess), which is the position of a string or char in a string. If find() could not find the string or char in a string, it returns npos, which is some maximum value. Not really sure what that means, but here:
http://www.cplusplus.com/reference/string/string/npos/

So, this means w is not counting the correct answers. w is holding the position of what is found, whether that be the position of an element of the string, or npos.
w should be counting the correct answers so it goes out of the loop... That is why I made it... ehhhh?
Lol. I'm not sure what you intended, haha. But it looks like w == the size of input, whatever that may be.
w is a variable I created which gets bigger by 1 each time a letter is found.
When the value of w is the same as input (the word) then it exits the loop.


My problem is, that I need to print out where the char (guess) is in the word so it looks like a proper hangman game.
Hum. I get it now.
1
2
3
4
5
while(w<input.size()){
    while(input.find(guess) < input.size()){
        w++;
    }
}


Does that help?
Last edited on
http://img.photobucket.com/albums/v334/Klaus5000/dasdasd.png
That is what it looks like. (h is replaced by each correct letter as it is the same position)

The code works...
I just need the position of the found chars so I can output them. Remember the game is hang man so the user needs to see the word as it builds up.


Then integrate your code with mine.

I'm not really sure what your code does up there, so I can't help you too much with finishing it.
Look at the picture.

The code outputs the correct letter... but i need to find the position it is in the string so it appears above the right dash. :)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

while(w!=input.size())
{
	crs->setPosition(xcd,ycd);
    cout << t << ": ";
    cin >> guess;
	
	if (input.find(guess) != string::npos)
	{ 
	crs->setPosition(xcd-12,ycd);
	cout << "hello there" << endl;

	crs->setPosition(xcd,ycd+3);
	cout << guess << endl;
	w++;
	}
	else
	{ 
	crs->setPosition(xcd-15,ycd);
	cout << "hello there NO" << endl; 
	w=w;
	}
}



the find() method is what gives you the position. Which is why i set w to it. Make a different variable for position and set it to input.find() then check to make sure it isn't longer than the string's length.

if( (p=input.find(guess)) < input.size()){}

is the same as

if( (p=input.find(guess)) < sting::npos){}

in terms of what you want.

Now, the reason I used while in my example is because you want to check for multiple letters. As long as p < input.size() OR npos, do the code in the block.
Last edited on
p is an int?

What should it do... Shall I leave all of my code and just change the if?

and then ++p and ++w ?

thanks

EDIT: What if the letters are the same?
P seems to get confused by the word "hello" as the 2 l's are there...

It works other than that.
Last edited on
Pages: 12