Replacing a letter with string find

Hello there! So I'm having a hard time replacing a character in a masked word based on the results of a string find function. The user guesses a letter, I determine whether or not its in the word with a string find function and then determine what index the correctly guessed letter is in. I'm pretty sure I'm not allowed to use mask[string.find(guess] in my code but I don't know how else I'm supposed to do it! /Does mask need to be a char array for it to work? Mask is declared as a string. This is only a portion of code from my hangman game. Thankyou for your time!

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

	cout << wordToGuess << endl; 
	//creating a masked version of the word to guess
	for (int i = 0; i < word.length(); i++)
	{
		mask += " _";
	}

	cout << "\nWord to guess: "<< mask;




			// Input Game Check(was the letter that they guessed in the word?)
			if (word.find(guess) != string::npos)
			{
				cout << "\nGREAT JOB!";
				cout << "\n" << word.find(guess) << endl;
				guess = toupper(guess);
				cout << "\n" << guess << " is in the word to guess!\n";
		 
				//Verifying that the found index that equals guess is correct
				cout << word.find(guess);
				
				//Setting the index in the string mask to equal the guess
				mask[word.find(guess)] = guess;

				//Displaying what should be the new masked word with the guessed letter revealed
				cout << mask << endl;

				++correctGuess;
	
			if (word.length() == correctGuess)
				{
					cout << "\nYOU WIN!\n" << endl;
					break;
				}
			else
			{
				cout << "" << endl; 
			}
			
			}
Why do you think you are not supposed to use mask[word.find(guess)];?
If you really dont want to use it, do a linear search on the word to get the index of guess.
I get an outside error, its like it just really doesn't like when I use mask[word.find(guess)]; for some strange reason. You basically just got down to the root of my question and re-asked it if that helps you understand :)
Last edited on
Try something like this.
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
#include <iostream>
#include <ctime>
#include <string>
#include <cstdlib>
#include <algorithm>
using namespace std;

int main()
{
	srand(static_cast<unsigned>(time(NULL)));
	const int n = 5;
	string word;
	string mask;
	char ch;
	
	string::iterator pos;

	int tries = 0;
	
	for(int i = 0; i < n; i++)
	{
		word.push_back(static_cast<char>(rand()%26 + 97));
		mask.push_back('-');
	}
	
	while(mask != word)
	{
		
		do
		{
			system("cls");
			cout<<mask<<endl;
			
			++tries;
			cout<<endl<<"Enter guess char;  # to quit : ";
			cin>>ch;
			
			if(ch == '#')
			{
				system("cls");
				cout<<"Sorry but thanks for trying..."<<endl;
				cout<<"The word is \""<<word<<"\" and you tried "<<tries<<" times"<<endl;
				return 1;
			}
			
			pos = find(word.begin(),word.end(),ch);
			
			if(pos == word.end())
			{
				cout<<"Sorry! "<<ch<<" not found in Hidden Word"<<endl<<endl;
				system("pause");
			}
			else if(mask.at(pos - word.begin()) != '-')
			{
				bool put = false;
				for(int i = 0; i < word.size(); i++)
				{
					if(word.at(i) == ch)
					{
						if(mask.at(i) == '-')
						{
							mask.at(i) = ch;
							put = true;
							break;
						}
					}
				}
				if(!put)
				{
					cout<<"You cannot guess the same character twice"<<endl<<endl;
					system("pause");
					--tries;
				}
			}
			else
			{
				cout<<"Good!!"<<endl<<endl;
				system("pause");
				mask.at(pos - word.begin()) = ch;
				break;
			}
		}while(1);
		
	}
	system("cls");
	cout<<"Congrats!!"<<endl;
	cout<<"The word is "<<mask<<endl;
	cout<<endl<<"Tries : "<<tries<<endl<<endl;
	
	system("pause");
	return 0;
}
Topic archived. No new replies allowed.