Somethings wrong...

So debugging my program,and this error shows up, along with many others for the different functions.
"'easy' : local function definitions are illegal
1> c:\users\hrikhu\documents\visual studio 2010\projects\game\game\main.cpp(26): this line contains a '{' which has not yet been matched"
I really don't understand what this means, and heres the code where the error occurs.

void easy ()
{
cout << "\nIn this game mode, easy, you must guess the secret number between 0 and 100.\n You have 5 guesses\n";
int wine=59; int guesse; int loop=0; guess: do { cin >> guesse;
if (guesse == wine) {cout << "\nYAY! You won!\n"; int main (); }
if (guesse < wine) {cout << "\nToo low\n"; loop=loop+1; goto guess;}
if (guesse > wine) {cout << "\nToo high\n"; loop=loop+1; goto guess;}
if (loop > 5) {cout << "\nSorry, you lost.\n"; break;}
} while (loop > 5);
}

This is like the other functions, so helping me fix this one would help a lot. Thanx.
In the statement

int wine=59; int guesse; int loop=0; guess: do

after guess a colon follows. I think that semicolon shall be. And this variable has no type specifier.

Also taking into account the first error message it seems that you defined your function inside the main..
Please review: http://cplusplus.com/doc/tutorial/control/

@vlad from moscow: 'guess' is a label.
It is very bad idea to call a label and a variable with identical names as in the code

int guesse; int loop=0; guess: do
The names are not identical. You see, the variable ends with an 'e' and the label does not.
However I have a new question.
When you define functions, like this:
"int play ();
void diff ();
void easy ();
void medium ();
void hard ();
int main ();
"
when you call the function do you end the function declaration with a semi colon such as this:
"void easy ();
{
cout << "\nIn this game mode, easy, you must guess the secret number between 0 and 100.\n You have 5 guesses\n";
int wine=59; int guesse; int loop=0; guess: do { cin >> guesse;
if (guesse == wine) {cout << "\nYAY! You won!\n"; int main (); }
if (guesse < wine) {cout << "\nToo low\n"; loop=loop+1; goto guess;}
if (guesse > wine) {cout << "\nToo high\n"; loop=loop+1; goto guess;}
if (loop > 5) {cout << "\nSorry, you lost.\n"; break;}
} while (loop > 5);
}
"
It seemed to have stopped all but a few easily fixed errors.
I use Microsoft 2010 Express.
Is it something specific to this compiler?
void easy(); declaration
easy(); call.
n = abs(n); another call example (now passing parameters and catching the returned value)
That said, you are not calling the 'easy' function in your program.

Check out http://cplusplus.com/doc/tutorial/functions/
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
void easy (bool &bresult)
{
cout << "\nIn this game mode, easy, you must guess the secret number between 0 and 100.\n You have 5 guesses\n";
int wine=59;  
int guesse = 0;  
int loop=0;

do { 
								cin >> guesse;
						if (guesse == wine)
							{
							      cout << "\nYAY! You won!\n"; 
								  bresult = true ;  //for calling the main()
                                                                break;

						      }
						if (guesse < wine)
							{ 
							cout << "\nToo low\n"; 
							loop=loop+1;

			
							}
						if (guesse > wine) 
							{
							cout << "\nToo high\n"; 
							loop=loop+1; 
			
							}
						if (loop > 5) 
							{
							cout << "\nSorry, you lost.\n"; 
							break;
							}
        } while (loop > 5);
}
int main()
{
	bool bresult = false;
	easy (bresult)
	if( bresult )
	{
		bresult =false; 
		main();
	}
}


I suppose this will help .
Last edited on
_ Horrible indentation
_ You do know that you can return values from functions, ¿right?
_ If it is not equal or less, it ought to be greater.
_ ¿Why are you checking loop>5 twice?
_ You can't call main(). It's against the standard.
Pardon my indentation. I didn't know that there is indentation in C++.
Yes, I know functions can return values. I wrote this when I was even more of a novice.
I also know that if i use more advanced C++, I can make this code 3 time shorter, but i wanna make this work first.
The new thing i learned is that there are indentations, and you cant call main ().
My code works now, it builds correctly, however there is an unwanted exit thing happening in my program.

Heres the code"
int main ()
{
cout << "Welcome to a C++ guessing game.\n";
cout << "Would you like to play\?\nY/N\?\n";
char yom;
cin >> yom;
if ((yom == 'n') | (yom == 'N')) {cout << "\nGoodbye!"; return 0;}
if ((yom == 'y') | (yom == 'Y')) {void diff ();}
else ((yom != 'n') | (yom != 'N') | (yom != 'y') | (yom != 'Y')); {cout << "\n"; int main ();}
}

\\and heres where the program goes if yom = y or Y

void diff ()
{
cout << "\nPlease choose difficulty:\nEasy (0-100)\nMedium (0-1000)\nHard (0-10000)\nE/M/H\?\n";
char diff;
cin >> diff;
if ((diff == 'e') | (diff == 'E')) void easy ();
if ((diff == 'm') | (diff == 'M')) void medium ();
if ((diff == 'h') | (diff == 'H')) void hard ();
else ((diff != 'e') | (diff != 'E') | (diff != 'm') | (diff != 'M') | (diff != 'h') | (diff != 'H'));
{cout << "\n"; void diff ();}
}
"
but instead,

if you enter anything, the program just close.
Help?
Hope this helps

// Comp070620121600.cpp : Defines the entry point for the console application.
//

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


void diff ()
{
	cout << "\nPlease choose difficulty:\nEasy (0-100)\nMedium (0-1000)\nHard (0-10000)\nE/M/H\?\n";
	char diff;
	cin >> diff;
	if ((diff == 'e') || (diff == 'E')) 
		easy ();
	else if ((diff == 'm') || (diff == 'M')) 
		medium ();
	else if((diff == 'h') || (diff == 'H')) 
		hard ();
	else 
	{
		cout << "\n"; 
		diff ();
	}
}
int _tmain(int argc, _TCHAR* argv[])
{
	cout << "Welcome to a C++ guessing game.\n";
	cout << "Would you like to play\?\nY/N\?\n";
	char yom;
	cin >> yom;
	if ((yom == 'n') | (yom == 'N')) {cout << "\nGoodbye!"; return 0;}
	else if ((yom == 'y') | (yom == 'Y')) { diff ();}
	else 
	{
		cout << "\n"; 
		main ();
	} 
	return 0;
}
Topic archived. No new replies allowed.