string function find.

Hi im making this hangman game, and i got the word they enter as a string and the letter they want to guess as a string, so i was wondering when i use the find function and say i have a word like jelly or something with double letters, what would find return? or how would i use find with the other erase and insert functions if there was double letters? thanks for any help.
closed account (S6k9GNh0)
http://cplusplus.com/reference/string/string/find/

As for dealing with n3wbs that want to use multiple letters, you may check to see if the given string size is greater than 1 using strlen or <string>.size();. Honestly, it shouldn't matter. When you check for the string, if the entire string isn't in the word, then it fails to find it. Say we have needle. If I use find to search for ned, it will find one occurance but no others meaning the other e is still hidden like a regular game. If you search for e, it will find the first one, then you have to call it again until npos flag is given. Have fun. :D
ok thanks man, i think thats just what i need. Im a little confused in the example it uses though and how it find the second needle, in

found=str.find("needles are small",found+1,6);

i dont get what the is the deal inside those brackets? thanks for any help once again.

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


int guess(string realword, int wordlength, int attempts, string solution)
{
	int temp;
	int count;
	int variable;
	string letter;
	string newsolution;

	variable = attempts;

	count = 1;
	while (count <= attempts)
	{
		cout << "Guess a letter (you have " << variable-- << " left): ";
		cin >> letter;
		
		temp = realword.find(letter);

		if (temp < 0 || temp > attempts)
		{
			cout << "Wrong, try again. Word so far is " << solution << endl;
		}
		else
		{
			solution.erase(temp, 1);
			solution.insert(temp, letter);

			newsolution = solution;

			cout << "Right! Word so far is " << newsolution << endl;
		}
			if (solution == realword || newsolution == realword)
			{
				cout << "Good job, you win!" << endl;
				return 1;
			}

		count++;	
	}

	cout << "Computer wins, you lose!" << endl;
	return 1;
}




Topic archived. No new replies allowed.