Hangman game function issue

Pages: 12
Apr 27, 2012 at 1:23am
p is an int because its holding a position.

However, I forgot that you need to make sure find isn't starting from the beginning. That's my bad. size_t find ( char c, size_t pos = 0 ) const;

Ok, so, make p 0 each time you search for a new letter, and change the if statement to:
if( (p=input.find(guess, p)) < string::npos){}

As you can see, we give it the value of p each time, which will start out at 0, then be the position of the next letter, and so on, until it becomes greater than the string size.
Apr 27, 2012 at 2:54am
Here is what I have done since we last spoke...

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
while(w!=input.size())
{
	if(crs)
	crs->setPosition(xcd,ycd+1);
	cout << "Press 1 to enter a word." << endl;
	if(crs)
	crs->setPosition(xcd,ycd);
    cout << t << ": " << endl;
	qin >> guess;
	if(guess=='1')
	{
		if(crs)
        crs->setPosition(xcd,ycd);
		cout << "Enter your word: " << endl;
		cin>>word2;
		if(word2.compare(input)!=0)
		{
			cout << "Wrong!" << endl;
			w=w;
		}
		else
		{
			cout << "Well Done!" << endl;
			w=input.size();
		}
	}
		

	//if (input.find(guess) != string::npos)
	//if( (p=input.find(guess)) < input.size())
	if( (p=input.find(guess, p)) < string::npos)
	{ 

	crs->setPosition(xcd-12,ycd);
	cout << "YES" << endl;
	
	crs->setPosition(xcd+p,ycd+6);
	cout << guess << endl;
	w++;
	}
	else if( (p!=input.find(guess)) < input.size())
	{ 
		if(crs)
	crs->setPosition(xcd-15,ycd);
	cout << "NO" << endl; 
	w=w;
	j++;
				if(crs)
				crs->setPosition(xcd,ycd);
	  					switch(j)
		{
			case 1: crs->setPosition(xcd-20,ycd); cout << "   |\              " << endl; break;
			case 2: crs->setPosition(xcd-20,ycd+1);cout << "   |--------|      " << endl; break;
			case 3: crs->setPosition(xcd-20,ycd+2);cout << "   |/       |      " << endl; break;
			case 4: crs->setPosition(xcd-20,ycd+3); cout << "   |        0      " << endl; break;
			case 5: crs->setPosition(xcd-20,ycd+4); cout << "   |       /|\\    " << endl; break;
			case 6: crs->setPosition(xcd-20,ycd+5);cout << "   |       / \\    " << endl; break;
			case 7: crs->setPosition(xcd-20,ycd+6); cout << " ______________    " << endl; break;
			case 8: crs->setPosition(xcd-20,ycd+7); cout << " YOU LOSE!    "; j=8;  break;
			default: break;
		}
		
	}
	
	




	}






The issue still remains with the letters like 'l' not appearing properly and merely printing 1 of the l's..

Also when you lose it does not exit properly...


Thanks so much. (See the comments for the various if statements I have used, the one I used before you posted just now works.)


Apr 27, 2012 at 2:56am
lol... Ok. I'll take a look at it and try to see what's going on.
Topic archived. No new replies allowed.
Pages: 12