Undeclared identifier error yet I declared?

Is this theory not wrong?
1
2
3
4
5
6
7
8
9
10
11
12
int main()
{
//Creates x
int x;
 {
 //Creates y
 int y;
 //Destroys y
 }
//destroys x
return 0;
}


Well I am having some sort of Undeclared Identifier error in my code:

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

int main()
{
	cout << "Welcome to the game of sticks!" << endl;
	int play = 1;
     int playerWin = 0;
     int botWin = 0;
	while (play == 1)
	{
		gameLoop();
		Sleep(1000);
		cout << "Current scores:\nPlayer: " << playerWin << "\nBot: " << botWin << endl;
		Sleep(500);
		for (int ask = 0; ask = 1;)
		{
		cout << "Would you like to play again? y/n" << endl;
		int y;
		cin >> y;
		switch(y)
		{
		case 'y':
		case 'yes': ++ask; break;
		case 'n':
		case 'no': play = 0; ++ask; break;
		default: break;
		}
		}

	}
	return 0;
}


void gameLoop()
{
	currentTurn Turn = TURN_PLAYER;

	int winner = WIN_NONE;

	while(winner = WIN_NONE)
	{

	if(Turn == TURN_PLAYER)
	{
		cout << "It is Player's Turn. How many sticks would you like to take? (1-3)" << endl;
		int ask = 0;
		while(ask == 0)
		{
			int x;
			cin >> x;
			if (x>= 1 && x<= 3)
			{
				stickNum-= x;
				++ask;
				Turn = TURN_BOT;
			}
			else
			{
				cout << "\nPlease enter a number from 1-3: " << endl;
			}
		}

		if(stickNum <= 0)
		{
			winner = WIN_BOT;
			cout << "You lose! You took the last stick!" << endl;
			++botWin;
		}
	}

	if(winner = WIN_BOT)
	{
		Turn = TURN_NONE;
	}


	if(Turn == TURN_BOT)
	{
		int x;
		x = thinkAI();
		stickNum-= x;
		switch(x)
		{
		case 1: cout << "The Bot removes :" << x << " stick" << endl; break;
		default: cout << "The bot removes :" << x << " sticks" << endl; break;
		Turn = TURN_PLAYER;
		}

		if(stickNum <= 0)
		{
			winner = WIN_PLAYER;
			cout << "You win! The Bot took the last stick!" << endl;
			++playerWin;
		}
	}

	if(winner = WIN_PLAYER)
	{
		Turn = TURN_NONE;
	}


	}
}


This is the error report:

1
2
3
4
5
6
7
1
1>Compiling...
1>Sticks.cpp
1>c:\users\hn\documents\visual studio 2008\projects\sticks\sticks\sticks.cpp(154) : error C2065: 'botWin' : undeclared identifier
1>c:\users\hn\documents\visual studio 2008\projects\sticks\sticks\sticks.cpp(180) : error C2065: 'playerWin' : undeclared identifier
1>Build log was saved at "file://c:\Users\hn\Documents\Visual Studio 2008\Projects\Sticks\Sticks\Debug\BuildLog.htm"
1>Sticks - 2 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


If you pay attention to the Underlined code, you can see that I declared the indentifiers yet they still not work?

Help will be appreciated, and I am still learning c++, its been my first month learning myself from internet tutorials

Local variables are local to the function you declare them in. If you declare something in main, gameLoop has now knowledge about it.
One solution would be to move the two variables to global scope. Then they would be visible form all functions. It's not great to use globals though. You'll know when your programs get larger.
A better solution would be to either pass those two variables to gameLoop as arguments (by reference) or use put them, and gameLoop in a class. If you don't know how to do either, go with a globals. You'll have time to learn.
Ok thanks, I thought since I declared them, I would have expected gameLoop to know. I will adjust my code and make it take in arguments.
Topic archived. No new replies allowed.