Problem with vector

So down here is my code, the meaning of this prog. is that I will have a word, type out a '_' for every char in theWord.
Everything is compiling like it should be but something is still wrong.
If I guess s, t, c it will type out: s_t_ _c.

What I want to do is instead of writing between the underscores it should replace the underscore with the guessed char.
Any tips and trix?

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
#include <iostream>
#include <vector>
#include <string>

int main(int argc, char** argv)
{
	std::string theWord;
	theWord = "stockholm";
	size_t found;
	char guess;

	
	std::vector<int> correctGuesses;
	std::vector<int> wrongGuesses;

	std::cout << "the word is " << theWord.length() << " chars long.\n\n";

	 while(theWord.length() > 0)
	 {

        for(unsigned int i = 0; i < theWord.length(); i++)
		{
			for(unsigned int j = 0; j < (int)correctGuesses.size(); j++)
			{
				if(theWord[i] == correctGuesses[j])
				{
					
					std::cout << theWord[i];
					continue;
					
				}
			
			}
				std::cout << " _ ";
		}
	
		
		std::cout << "Please guess a char: " << std::endl;
		std::cin >> guess;
		std::cout << "\nYou guessed: " << guess << "\n\n";
		found=theWord.find(guess);

		
		if(int(found) != std::string::npos)
		{
			correctGuesses.push_back(guess);
			std::cout << "'" << guess << "'" << " was found at position: " << int(found);
			std::cout << "\ncorrectGuess holds: " << (int)correctGuesses.size() << " char/s.\n\n";

			for(unsigned int i = 0; i < theWord.length(); i++)
			{
				
				if(guess == int(found)) //not sure about this one.
				{
					
					theWord.replace(theWord.begin(), theWord.end(), int(found), guess);
				}
				
			}
			
		}
		else
		{
			std::cout << "sorry nope!\n";
			wrongGuesses.push_back(guess);
			std::cout << "\nwrongGuesses holds: " << wrongGuesses.size() << " char/s.\n";
	    }
	 }
	return 0;
}


Yeeaa.. bumping this now.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
        for(unsigned int i = 0; i < theWord.length(); i++)
		{
			bool guess_ok = false; // You need this variable in order to determine if you have to output the '_'
			for(unsigned int j = 0; j < (int)correctGuesses.size(); j++)
			{
				guess_ok = (theWord[i] == correctGuesses[j]); // Now set the variable
				// if(theWord[i] == correctGuesses[j])
				if(guess_ok)
				{
					
					std::cout << theWord[i];
					break; // NOTE: You don't want to continue; if the guess is ok
					
				}
			
			}
			if(! guess_ok) // Note: Only if not guess ok output the '_'
				std::cout << " _ ";
		}
thank.. you! :)
Topic archived. No new replies allowed.