Char Array Update

I am having a really hard time updating the array for char. I am making hangman game, in which
char board[WORD_LENGTH+1] = "-----";
and in the following function every time user makes a right guess board needs to update the letters precisely to match the words, but leave rest of the words until next guess is made.

Below is what i have but it only updates the first word 5 times and then nothing happens in the next guess.

Please help thanks

1
2
3
4
5
6
7
8
9
10
11
12
// Update the board array to put the char guess whereever
// it appears in the word array.  For example, if the word equals
// "every", board equals "-----", and guess equals "e", then
// board should be updated to equal "e-e--"
void updateTemplate(char guess, char word[], char board[])
{
    	for (int n=0; n < WORD_LENGTH ; n++)
	{
	if ( word[n] == guess  )
	cout << guess<< board[n]<< endl; 
         }
}
Last edited on
if ( word[n] = guess )

Are you doing an assignment or a comparison for above line ? Assign is = whereas comparison of a char is ==
its a comparison it was typo error, i corrected it thanks.
i have made few changes but its still not updating the board, any help is appreciated

thanks
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void updateTemplate(char guess, char word[], char board[])
{
			if ( word[0] == guess)
			board[0] == word[0];
			else if ( word[1] == guess)
			board[1] == word [1]; 
			else if ( word[2] == guess)
			board[2] == word [2];
			else if ( word[3] == guess)
			board[3]== word [3];
			else if ( word[4] == guess)
			board[4]== word [4];

	
}

Last edited on
closed account (D80DSL3A)
The characters in board[] won't be changed in either version of your function. There are no assignments being made. eg.board[0] == word[0]; is not an assignment. Use = not == for assignment. sohguanh explained that already.
Also, your 2nd version wouldn't catch the 2nd 'e' in your example. The loop in your 1st version would though if an assignment were made there.
it worked beautifully

thanks fun2code
Topic archived. No new replies allowed.