Modified Hangman Game

Make a program that is like a hangman but there are only 3 words to pick randomly by the program and outputs the word. The word will change its 4 letters randomly into underscores. User will answer by character - if the character input is right then outputs "One match!" else "No match!"and there are 3 tries only. If user gets the right 4 characters then program outputs Tsamypon!
- My problem is that sometimes it says "Wrong!" even if the character input is in the word and says "One match!" only 1 time if it's right and then followed by "Wrong!".

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
#include <iostream>
#include <ctime>
#include <cstring>
#include <cstdlib>
using namespace std;

int main(){
	
	const string word1 = "COMPUTING", word2 = "INFORMATION", word3 = "SCIENCE";
	string holdword;
	
	
	srand(time(0));
	int randword = rand() % 3;
	
	if (randword == 0){		
		
		holdword = word1;
	}
	else if (randword == 1){	
		
		holdword = word2;
	}
	else holdword = word3;		
	
	
	int length = holdword.length() - 1;
	string strhold[length + 1];
	for (int a = 0; a <= length; a++){
		
		strhold[a] = holdword.at(a); 
	} 
	
	
	for(int a = 0; a < 4; ){
		
		int rblank = rand() % length;
		if(strhold[rblank] != " _ ")	
		{
			strhold[rblank] = " _ ";	 
			a++;
		}
    }
    
    cout<<"\nWord to guess: ";
    for (int a = 0; a <= length; a++){
    	
		cout<<strhold[a];	 
	}
	cout<<endl;
    
	
int answer = 0, wrong = 0;
	char userguess;
	
	while (answer != 4 || wrong != 3){
		cout<<"\nEnter guess: ";
		cin>>userguess;
		
		for (int a=0;a<=length;a++){
			if (userguess == holdword.at(a)){
				if (strhold[a] == " _ "){
					strhold[a] = userguess;
					cout<<"One match! "<<endl;
					answer++;
					break;
				} 
			}
			else if (userguess != holdword.at(a)){
				cout<<"No match! "<<endl;
				wrong++;
				break;
			}
			else continue;
		}
		if (wrong == 3){
			cout<<"\nGame over! "<<endl;
			break;
		}
		else if (answer == 4){
			cout<<"Tsampyon! "<<endl;
			break;
		}
		else continue;
	}
	
	system("pause");
	return 0;
}
	
Last edited on
Now I got on how to check if the input is right or wrong but my new problem is that the program only says right if the answer is equal to the first blank of the word. I want the program to say right if the input is in the word even if it's input is not equal to the first blank but on the other blanks.
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
#include <iostream>
#include <ctime>
#include <cstring>
#include <cstdlib>
using namespace std;

int main(){
	
	const string word1 = "COMPUTING", word2 = "INFORMATION", word3 = "SCIENCE";
	string holdword;
	
	
	srand(time(0));
	int randword = rand() % 3;
	
	if (randword == 0){		
		
		holdword = word1;
	}
	else if (randword == 1){	
		
		holdword = word2;
	}
	else holdword = word3;		
	
	
	int length = holdword.length() - 1;
	string strhold[length + 1];
	for (int a = 0; a <= length; a++){
		
		strhold[a] = holdword.at(a); 
	} 
	
	
	for(int a = 0; a < 4; ){
		
		int rblank = rand() % length;
		if(strhold[rblank] != " _ ")	
		{
			strhold[rblank] = " _ ";	 
			a++;
		}
    }
    
    cout<<"\nWord to guess: ";
    for (int a = 0; a <= length; a++){
    	
		cout<<strhold[a];	 
	}
	cout<<endl;

	
int answer = 0, wrong = 0;
	char userguess;
	
	while (answer != 4 || wrong != 3){
		cout<<"\nEnter guess: ";
		cin>>userguess;
		
		for (int a=0;a<=length;a++){
			if (userguess == holdword.at(a)){
				if (strhold[a] == " _ "){
					strhold[a] = userguess;
					cout<<"One match! "<<endl;
					answer++;
					break;
				} 
			}
			else if (userguess != holdword.at(a)){
				if (strhold[a] == " _ "){
					cout<<"No match! "<<endl;
					wrong++;
					break;}
			
		else continue;}
			
		}
		if (wrong == 3){
			cout<<"\nGame over!\n "<<endl;
			break;
		}
		else if (answer == 4){
			cout<<"Tsampyon!\n "<<endl;
			break;
		}
		else continue;
	}
	
	system("pause");
	return 0;
}
Last edited on
jammes,

in the while loop you have the for (int a=0;a<=length;a++) , but when you found the first one, you break out of the for loop. The same happens if the first blank is not a match.

If you have the same letter twice, you only count it once if it's the first one and then break the loop. for instance SCIENCE -> S _ I _ N _ _ is only missing the 'C' and the 'E'.

Suggestion: how about instead of incrementing answer or wrong in the for (int a=0;a<=length;a++), you create an int match variable and increment that one in the loop if it's " _ " without breaking the loop, and then, after the for loop, you check if match is 0 and increment wrong++ or otherwise answer += match. That way, if there are more than one matches, you have them covered...

Also, you do a lot of extra work with length:
1
2
3
    int length = holdword.length() - 1;
    string strhold[length + 1];
    for (int a = 0; a <= length; a++)

instead, how about
1
2
3
4
        int length = holdword.length();  // no - 1
        string strhold[length];          // no + 1
        for (int a = 0; a < length; a++) // same for all for(int a = 0;...) loops
  //not for (int a = 0; a <= length; a++) 

although it has the same result, it is more intuitive since the array is 0 (zero) indexed, meaning it starts with strhold[0] and ends with strhold[strhold.length() - 1], so a < length will go to a maximum of length - 1.
Eeeeyy haha, you're here!

Someone actually made a question earlier than I did, only a few hours ahead.

Thanks man, you helped a lot of us

Last edited on
Thanks RobiBue. Now there is another problem,
The output is like this:
=====================
Word to guess: S _ IE _ _ _

Enter guess: C
One match!
No match!
One match!
No match!

Enter guess:
=====================
RobiBue I only want the output once like One match if its input is correct otherwise No match and not like that above. :(
Last edited on
Now there is another problem,
The output is like this:
=====================
Word to guess: S _ IE _ _ _

Enter guess: C
One match!
No match!
One match!
No match!

Enter guess:
=====================

Jammes, this happens because you are now going through the for loop and since C appears twice where there are " _ " you get 2 x "One match!" and since neither N nor E == C you get 2 x "No match!"

my suggestion:
take the else if (userguess != holdword.at(a)){... completely out.

1
2
3
4
5
6
7
8
9
10
11
12
13
int answer=0, wrong=0, match;

match=0;
for (i=0;i<length;i++){
  if (guess==holdword[i] && strhold[i]==blank){
    strhold[i]=guess; match++;
  }
}
if (match==0){ std::cout << "No match!\n"; wrong++; }
else{
  std::cout << match << (match==1?" match!":" matches!") << std::endl;
  answer += match;
}


see if this logic helps you...
I don't really understand that semicolon but it works! :D . Another problem is that wrong doesn't do his job. Even if its value is 3 or more than that the program still continues to output Enter guess. Why is that?
Last edited on
Thanks RobiBue! Everything is working now. :D
Topic archived. No new replies allowed.