help in hangman game

I am trying to make a function that checks if a letter is in a word. I added the for-loop so that it can find all the letters. At the moment, it prints the same pos and doesn't increment.

Thanks in advance!

1
2
3
4
5
6
7
8
9
10
11
12
13
    void checker(string word, char guess)
    {
        size_t pos;

        for(int i=0; i < word.length(); i++)
        {
            pos=word.find(guess);
            if(pos!=string::npos)
            {
                cout << "CORRECT! Found at " << pos << endl;
                pos++;
            }
        }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <string>

using namespace std;

int main() {
	string word = "test";
	
	char a = 't';
	
	size_t pos = 0;
	while((pos = word.find(a, pos)) != string::npos)
	{
		cout << a << " found at position " << pos << endl;
		++pos;
	}
}
post the entire code of your program so tht others can help.For example,your main function,the class/body function tht uses void checker,etc.
Topic archived. No new replies allowed.