Letter guessing game, errors of the code that i do not understand

Oct 3, 2011 at 2:48am
//Guesses letter game

#define _CRT_SECURE_NO_DEPRECATE
#include <stdio.h>
#include <ctype.h>

#define MAX_GUESSES 6

//function prototypes

int main()
{
FILE* infile;
int i;
int numberofgames;
char ans;
char getguess;
int compareguess(char guess, char ans);
printf("Please enter the no of games you want to play");
scanf("%d",& numberofgames);
//variable declarations
//prompt + get numgames
//file pointer declaration
//display instructions on how to play the game
printf("Enter an letter in the range a to z\n");
printf("I will try to guess your letter with in 6 guesses\n");
printf("If you can guess my letter in 6 guesses, You Win!\n");
printf("Would you like to play?\n");
printf("Press 'Y' to play, or 'N' to quit\n");
printf("Char Playgame\n");

//connect to the file
infile = fopen("letters.txt", "r");

return 0;
}
for(i=1; i<=numberofgames; i++)
{
//print current game (value of i)
//get letter to guess from fscanf
fscanf(infile," %c",&ans);
//printf to check the letters from file
printf("%c",&ans);
//call the play function to play a game
numgames=playgame (ans);
//it will return a 1 if the user wins and a 0 if the user took more than 6 guesses
//condition here to print win or lose
numgame==0'lose';numgame==1'win';

int playgame (char ans);
{
char guess;
numberofguess=0, win or lose=0;
//call the play function to play a game
numgames=playgame (ans);
//it will return a 1 if the user wins and a 0 if the user took more than 6 guesses
//condition here to print win or lose
numgame==0'lose';
numgame==1'win';
}
return 0;
}

char GetGuess()
{
//get the users' guess
userguess = tolower(userguess);//change to lower case
//return the users' guess
return 0;
}

int compareguess(char guess, char ans)
{
//compare the guess and the answer
compare guess and ans;
//return a 1 if they are the same
return a 1 if both are the same;
//print a message based on before or after alphabetically
printf("message based on before or after alphabetically",&ans);
//return a 0 if the guess and answer are not the same
return a 0 if the guess and answer are not the same;

return 0;
}
Last edited on Oct 3, 2011 at 2:48am
Oct 3, 2011 at 3:39am
1. Please use [code]//code goes here [/code] tags.
2. Please read http://www.cplusplus.com/forum/beginner/1/ . Basically we need detailed information, including a verbatim of the error messages. Don't make us guess what the errors are: Tell us.
Oct 3, 2011 at 3:48am
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

//Guesses letter game

#define _CRT_SECURE_NO_DEPRECATE
#include <stdio.h>
#include <ctype.h>

#define MAX_GUESSES 6

//function prototypes

int main()
{
FILE* infile;
int i;
int numberofgames;
char ans;
char getguess;
int compareguess(char guess, char ans);
printf("Please enter the no of games you want to play");
scanf("%d",& numberofgames);
//variable declarations
//prompt + get numgames
//file pointer declaration
//display instructions on how to play the game
printf("Enter an letter in the range a to z\n");
printf("I will try to guess your letter with in 6 guesses\n");
printf("If you can guess my letter in 6 guesses, You Win!\n");
printf("Would you like to play?\n");
printf("Press 'Y' to play, or 'N' to quit\n");
printf("Char Playgame\n");

//connect to the file
infile = fopen("letters.txt", "r");

return 0;
}
for(i=1; i<=numberofgames; i++)
{
//print current game (value of i)
//get letter to guess from fscanf
fscanf(infile," %c",&ans);
//printf to check the letters from file
printf("%c",&ans);
//call the play function to play a game
numgames=playgame (ans);
//it will return a 1 if the user wins and a 0 if the user took more than 6 guesses
//condition here to print win or lose
numgame==0'lose';numgame==1'win';

int playgame (char ans);
{
char guess;
numberofguess=0, win or lose=0;
//call the play function to play a game
numgames=playgame (ans);
//it will return a 1 if the user wins and a 0 if the user took more than 6 guesses
//condition here to print win or lose
numgame==0'lose';
numgame==1'win';
}
return 0;
}

char GetGuess()
{
//get the users' guess
userguess = tolower(userguess);//change to lower case
//return the users' guess
return 0;
}

int compareguess(char guess, char ans)
{
//compare the guess and the answer
compare guess and ans;
//return a 1 if they are the same
return a 1 if both are the same;
//print a message based on before or after alphabetically
printf("message based on before or after alphabetically",&ans);
//return a 0 if the guess and answer are not the same
return a 0 if the guess and answer are not the same;

return 0;
} 


Errors

(14): warning C4101: 'i' : unreferenced local variable
(17): warning C4101: 'getguess' : unreferenced local variable
(16): warning C4101: 'ans' : unreferenced local variable
(37): error C2059: syntax error : 'for'
(37): error C2143: syntax error : missing '{' before '<='
(37): error C2059: syntax error : '<='
(37): error C2143: syntax error : missing '{' before '++'
(37): error C2059: syntax error : '++'
(37): error C2059: syntax error : ')'
(67): error C2065: 'userguess' : undeclared identifier
(67): error C2065: 'userguess' : undeclared identifier
(75): error C2065: 'compare' : undeclared identifier
(75): error C2146: syntax error : missing ';' before identifier 'guess'
(75): error C2146: syntax error : missing ';' before identifier 'and'
(75): error C2065: 'and' : undeclared identifier
(75): error C2146: syntax error : missing ';' before identifier 'ans'
(77): error C2065: 'a' : undeclared identifier(77): error C2143: syntax error : missing ';' before 'constant'
(77): error C2143: syntax error : missing ';' before 'if'
(77): error C2061: syntax error : identifier 'both'(81): error C2065: 'a' : undeclared identifier
(81): error C2143: syntax error : missing ';' before 'constant'
(81): error C2143: syntax error : missing ';' before 'if'
(81): error C2061: syntax error : identifier 'the'
Oct 3, 2011 at 6:19am
Lines 38 to 49 (in this post) are outside a function. They need to be inside a function.

In line 68, you try to use a variable called "userguess" that you did not define.

I suppose that line 76 is meant to be a comment line??? Same for line 78 and 82.
Oct 3, 2011 at 6:49am
How about this?

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
 

//Guesses letter game

#define _CRT_SECURE_NO_DEPRECATE
#include <stdio.h>
#include <ctype.h>

#define MAX_GUESSES 6

//function prototypes
	int i;
	int numberofgames;
	char ans;
	char getguess;
	int compareguess(char guess, char ans);

int main()
{
	FILE* infile;
	//int i;
	//int numberofgames;
	//char ans;
	//char getguess;
	//int compareguess(char guess, char ans);
	printf("Please enter the no of games you want to play");
	scanf("%d",& numberofgames);
	//variable declarations
	//prompt + get numgames
	//file pointer declaration	
	//display instructions on how to play the game
	printf("Enter an letter in the range a to z\n");
	printf("I will try to guess your letter with in 6 guesses\n");
	printf("If you can guess my letter in 6 guesses, You Win!\n");
    printf("Would you like to play?\n");
    printf("Press 'Y' to play, or 'N' to quit\n");
    printf("Char Playgame\n");

	//connect to the file
	infile = fopen("letters.txt", "r");

	return 0;
}
	for(i=1; i<=numberofgames; i++)
	{
		//print current game (value of i)
		//get letter to guess from fscanf
		fscanf(infile," %c",&ans);
		//printf to check the letters from file
		printf("%c",&ans);
	
	int playgame (char ans); 
	{
		char guess;
		numberofguess=0, win or lose=0;
		//call the play function to play a game
		numberofgames=playgame (ans);
		//it will return a 1 if the user wins and a 0 if the user took more than 6 guesses
		//condition here to print win or lose
		numberofgames==0'lose';
		numberofgames==1'win';
	}
	return 0;
}


All the errors deal with the line of the for function:
(42): error C2059: syntax error : 'for'
(42): error C2143: syntax error : missing '{' before '<='
(42): error C2059: syntax error : '<='
(42): error C2143: syntax error : missing '{' before '++'
(42): error C2059: syntax error : '++'
(42): error C2059: syntax error : ')'
Oct 3, 2011 at 7:06am
Lines 44 to 50 do not belong to the function main() or any other function. They need to belong to a function.
Oct 3, 2011 at 7:14am
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
//Guesses letter game

#define _CRT_SECURE_NO_DEPRECATE
#include <stdio.h>
#include <ctype.h>

#define MAX_GUESSES 6

//function prototypes
	int i;
	int numberofgames;
	char ans;
	char getguess;
	int compareguess(char guess, char ans);

int main()
{
	FILE* infile;
	//int i;
	//int numberofgames;
	//char ans;
	//char getguess;
	//int compareguess(char guess, char ans);
	printf("Please enter the no of games you want to play");
	scanf("%d",& numberofgames);
	//variable declarations
	//prompt + get numgames
	//file pointer declaration	
	//display instructions on how to play the game
	printf("Enter an letter in the range a to z\n");
	printf("I will try to guess your letter with in 6 guesses\n");
	printf("If you can guess my letter in 6 guesses, You Win!\n");
    printf("Would you like to play?\n");
    printf("Press 'Y' to play, or 'N' to quit\n");
    printf("Char Playgame\n");

	//connect to the file
	infile = fopen("letters.txt", "r");

	for(i=1; i<=numberofgames; i++)
	{
		//print current game (value of i)
		//get letter to guess from fscanf
		fscanf(infile," %c",&ans);
		//printf to check the letters from file
		printf("%c",&ans);
	return 0;
	}

	int playgame (char ans); 
	{
		char guess;
		("numberofguess=0"), ("winorlose=0");
		//call the play function to play a game
		numberofgames=playgame (ans);
		//it will return a 1 if the user wins and a 0 if the user took more than 6 guesses
		//condition here to print win or lose
		numberofgames==0("lose");
		numberofgames==1("win");
	
	return 0;
}


errors:
(50): error C2143: syntax error : missing ';' before 'type'
(55): warning C4013: 'playgame' undefined; assuming extern returning int
(58): error C2064: term does not evaluate to a function taking 305 arguments
(59): error C2064: term does not evaluate to a function taking 305 arguments
(63): fatal error C1075: end of file found before the left brace '{' at 'c:\users\addysonjones\documents\visual studio 2010\projects\guessletters\guess.c(17)' was matched





Oct 3, 2011 at 7:33am
Line 53 is not doing anything. Line 55 produces recursion but there's no condition to avoid a stack overflow. Lines 58 and 59 are not valid C++.

You are also missing the closing brace of the for loop in line 40. You open a brace in line 41 but you don't close it. This is one of the reasons to use proper indentation: To easily see where you are missing stuff like closing braces.
Last edited on Oct 3, 2011 at 7:37am
Oct 3, 2011 at 7:35am
ok, can you help me word them the way they should be? Im completely new at this and still a bit lost
Oct 3, 2011 at 7:39am
Well, I cannot fix playgame() if I don't know its purpose and intended return values. As it is right now, I think you want to use it to determine if the user won or lost. But you clearly have a lot of wrongs in the very basics of C++. I recommend that you study the basics before moving on to functions and the like.

For example, it seems that you want to return 0 or 1, but you always return 0 (see line 61).
Oct 3, 2011 at 7:45am
yes i believe i do want to do that. This is all play game is: //this function runs one game. It for checks either 6 incorrect guesses or correct guess.
//It returns a 0 if the game is over and the player did not guess the letter, otherwise it returns 1.
int PlayGame(char answer);
Topic archived. No new replies allowed.