error: w8004, why oldCorrect++ doesnt work

this is a function of a guessing game,
I need to print the guessed letter for the puzzle like: _a_
OR print the used letters: c, j, k, etc.

hope anyone can help.

cheers
AI caramba!!

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
 void guess_letter (char word_player[], int word_size, char underscore[],char letter_used[])
{		
	char guess[4]; // why i cannot put [word_size]
	char LetterEntered,valid;
	int x;
	int numCorrrect = 0;
	int oldCorrect = 0;
	do{
		printf("Enter a guess letter: ");
		fgets(guess, word_size, stdin);
		LetterEntered = guess[0];
		oldCorrect = numCorrrect;
		
		if(LetterEntered >= 'a' && LetterEntered <= 'z')
		{	
			valid = 'y';
			for (x=0; x < word_size; x++)
			{
				if(word_player[x] == LetterEntered)
				{
					underscore[x] = LetterEntered;
					printf("Now the word is: %s ", underscore);
					break;
				}
				else
					if(word_player[x] != LetterEntered)
					{	
						letter_used[x] = LetterEntered;
						printf("letters used %s \n", letter_used); 
						x++;
						break;
					}			
			}
		}
		else
			printf("Enter a char.\n");
			
	}while(valid != 'y');
	return;
} 
why i cannot put [word_size]?

Because C++ doesn't have variable-length arrays. The size of arrays with automatic storage duration must be known at compile time. http://en.wikipedia.org/wiki/Variable-length_array
why there is an error w oldCorrect++?

Peter87 just explained to you whats wrong.

Array sizes need to be known at compile time therefore you cant allocate an array in that way.

What you would need to do is dynamically allocate memory for your word using the new operator: http://www.cplusplus.com/reference/new/operator%20new/

1
2
3
4
5
6
7
8

char *wordBuffer;
wordBuffer = new char[size];

// ... do what you need to do

delete[] wordBuffer;  // free the memory


I wrote a simple hangman game for another user that uses the new operator to allocate a memory buffer as per the size of the word: http://www.cplusplus.com/forum/beginner/131952/#msg711121 - this should give you some ideas.

Topic archived. No new replies allowed.