Cannot solve this issue with Hangman Game

I am building a hangman game using parallel arrays, most of this was given by my professor, but i am struggling with trying to go through the array and check for correct guessed letter. Then i am supposed to change my array with the *'s to the guessed letter if it is correct. Main issue is under the comment //walk through array and check for miss. Any help is appreciated?
Last edited on
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
93
94
95
96
97
98
99
100
101
102
103
104
105
#include<iostream>
#include<cstdlib>
#include<ctype.h>
#include<iomanip>
#include<conio.h>
#include<cstring>

using namespace std;

int main(void)
{
	int misses = 0, missesA = 0; 
	char WordArray[2][20];
	char UseEnter;
	int Count = 0, i;
	bool goodGuess = false;
	char letter, usrp;
	
	system("color f4");
	 do
	 {
		WordArray[0][0] = ' \0';
		WordArray[1][0] = ' \0';
		if (Count)
			cin.ignore();
		Count = 0;
		do
		{
			system("CLS");
			cout << "\n\n\n";
			cout << "\t* * * HANGMAN * * * (Brenton Pugh)\n\n";
			cout << "\n\tEnter Word: ";
			i = 0;
			while(WordArray[0][i] != '\0')
			{
				cout << WordArray[0][i] << ' ';
				i++;
			}
			UseEnter = toupper(getch());
			
			if(int(UseEnter) != 13 && int(UseEnter) >= 65 && int (UseEnter) <= 90)
			{
				WordArray[1][Count] = UseEnter;
				WordArray[1][Count + 1] = '\0';
				WordArray[0][Count] = '*';
				WordArray[0][Count + 1] = '\0';
				Count++;
			}
		}while(int(UseEnter) != 13);
		
		// Just to show what is in the Array comment this out before final.
	//	cout << "\n\n" << WordArray[1];
	//	getch();
	
		do
		{
			system("CLS");
			cout << "*** HANGMAN - GAMETIME ***" << endl;
		
			//number of misses
			while(WordArray[i] == '\0')
			{
				i++;
				Count++;
				
			}
		
			if(Count <= 5)
				missesA = 2;
			else if(Count <= 10)
				missesA = Count - 3;
			else if(Count > 10)
				missesA = 8;
			
			//ask user for letter
			cout << "Number of Misses: " << misses << " out of " << missesA;
			
			cout << "\n\n" << WordArray[0] << endl << endl;
	
			cout << "Enter a letter: ";
			cin >> letter;
			letter = toupper(letter);
			
			//walk through array and check for miss
			for(int i = 0; i < '\0'; i++)
			{
				if(strcmp(WordArray[0], WordArray[i]) == 0)
					WordArray[0] == WordArray[i];
					goodGuess = true;
				if(!goodGuess)
					++misses;
				
			}
		
		}while(misses != missesA);
	
		if(misses = missesA)
			cout << "YOU LOSE! Word was: " << WordArray[1];
	
	
	cout << "Do another? (y/n)";
	cin >> usrp;
	}while(usrp = 'y' || 'Y');

}
Last edited on
Also NOTE: I am not finished with this yet, and am still a beginner programmer. Just need help with checking and changing correct guessed letters.
closed account (48T7M4Gy)
Instead of 105 lines of stuff why not write a short program to test the functionality at hand. You're basically wasting your (not my) time by going through what is in effect the same pile of rubbish each time to get at the real issue.

Once you've got a short program separately designed and 'running' re-incorporate it back into the main program and move on to the next problem.

Hard code instead variables instead of getting user input each time too, if you can.

The problem you're experiencing probably won't take longer than 30 minutes to sort out if you follow the 'narrow down' approach. Probably days or weeks if you don't.

You won't be waiting so long for help that way either. :)
Lines 22 & 23: ' \0' should be '\0' (no space after opening quote).

Line 97: = should be == (= is the assignment operator == tests for equality).

Line 103 doesn't do what you think it does. You're using = instead of == again, but even so, if you said usrp == 'y' || 'Y' it would be the same as (usrp == 'y') || 'Y' and since 'Y' is non-zero (aka "true"), the expression would always be true. To solve this, use toupper: (toupper(usrp) == 'Y')

To check for a good letter, just use strchr(). strchr(const char *s, int c) looks for character c in the string s. It returns a pointer to the first occurence of c in s, or NULL if c isn't found, so you could use:
1
2
3
4
goodGuess = strchr(WordArray[0], letter);
if(!goodGuess) {
   ++misses;
}


Next, I follow kemort's advice: concentrate on just the code that enters the word. Get that working first. HINT: use gets(). Then move on to the code that guesses the word.
Thanks guys for all the help! I'm a beginner as I said, so I am only in my first c++ class and my professor moves so quickly through the material I basically have to try and teach myself! I appreciate all the help!
Topic archived. No new replies allowed.