Replacing the lettter

I wrote a function for hangman its still in the works. But I have run into a problem with replacing my letters from my secret word (******) to (J*****).
Or whatever the word is. He she is ...

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
// Hangman function
void PlayHangman(string str1)

{

	char letter=' ';
	string secret="";
	bool match= false;
	

	secret=str1;
	
	for(string::size_type i=0; i < str1.size(); i++)
	secret.at(i)='*';
	cout << "User word is " << secret << endl;
	
	
	int counter=0;
	
	while(counter!=7)
	{
	
	cout << "Enter a letter " << endl;
	
	cin >> letter;
	
	
	
	for(string::size_type i=0; i < str1.size();i++)
	{
		if(str1.at(i)==letter)
		{
			secret.at(i)=letter;
			match=true;

		}
	
	}
		
		
	



	}		
	

}



The problem I think it the last cluster of code. This is supposed to replace the secret word at i with the letter but it is not working. Can someone also tell me what exactly :: is in the str1::size_type I used it from an example my teacher gave without knowing exactly why.

Anyways that last cluster of code is giving me a hard time.

Thanks,
J
D*MMIT! I got it. Sorry but still can someone help me understand what the :: thing is all about I wrote about above also in the function heading my teacher used const string original what is this about also?

Thanks
string::size_type means that defined type size_type belongs to class string.
As for your function it is better to declare the parameter as const reference to sttring because 1) it is not changed in the function body 2) there is no need to call a constructor to create the parameter that is a local variable of the function.

I would rewrite the function the following way

1
2
3
4
5
6
7
8
9
10
void PlayHangman( const string &str1 )

{

	char letter=' ';
	string secret( str1.size(), '*' );
	bool match= false;
	

	cout << "User word is " << secret << endl;
OK I did that it that was a good idea. I know this is is a more of the same but :: is the scope resolution operator right ? In a sense when I have string:: size type_ i=0; I'm just defining i to be a string? But Is this :: still the scope resolution operator? Or is it something different in this case .... Sorry just trying to get it all straightened out.

Thanks for advice above.
std::string and string::size_type are different things. Read at last some book on C++!
How come this string secret( str1.size(), '*' );is not something that is initialized like string hello ="";

Sorry I'm not so good at programming but still why is this ?
I read it is just confusing so when you use string::size_type the :: in this case isn't the scope resolution operator?
And you didn't answer about my question regarding
string secret( str1.size(), '*' );
All this is declaring a variable right? So how come you don't have to initailize it to somthing like this .... string secret =( str1.size(), '*' ); Thats what I want to know.



Sorry dude I try :) I'm looking at my book right now!
Topic archived. No new replies allowed.