identifier is undefined

is anyone able to explain to me why line 22 and 23 give off the identifier is undefined, I dont understand it. I did everything the way it was intended and I cant give those identifiers values since they have a specific use.

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
  /******************************************
A              *
C                                  *
H								  *
4/8/2018/                                 *
******************************************/

#include <iostream>
#include <iomanip>
#include <cstdlib>
using namespace std;

int main()
{
	//functions
	int beginGame(int playedBoard[]);
	void displayBoard(int board[][3]);
	bool testWinner(int ans, int boardNum, int ansBoard);
	void instructions();

	instructions();
	beginGame(playedBoard);
	displayBoard(board);

	//
	const int max_amount_of_guesses = 3;
	int guessesMade = 0;
	do
	{
		//AskForGuess();
		guessesMade++;

	} while (guessesMade < max_amount_of_guesses);

}

int beginGame(int playedBoard[])
{
	//constants for the number of rows and columns
	//understood this part
	const int row_size = 4;
	const int col_size = 3;

	int boardNum1[row_size][col_size] = { { 90, 9 , 45 },
	{ 66, 12, 48 },
	{ 34, 7 , 70 },
	{ 44, NULL , 26 } };

	int boardNum2[row_size][col_size] = { { 28, 10, 55 },
	{ 89, 17, 98 },
	{ 22, 4 , 31 },
	{ 69, NULL , 78 } };

	int boardNum3[row_size][col_size] = { { 38, 11, 83 },
	{ 15, 6 , 33 },
	{ 11, 2 , 20 },
	{ 86, NULL , 95 } };

	int ansBoard[3] = { 8, 15, 14 };
	int usedBoards[3] = { NULL };

	//this will allow a random board to be genterated on the board
	int maxValue = 3;
	int minValue = 1;
	int random_board_number = (rand() % (maxValue - minValue + 1)) + minValue;
	bool testWinner(random_board_number);
}

bool testWinner(int ans, int boardNum[], int ansBoard[])
{
	//when the user wins boardNum[random_board_number]=true
	int current_guess = 0;
	int guess_count = 0;

	if (boardNum[0] && boardNum[1] && boardNum[2] == true)
	{
		cout << "You are the winner!" << endl;
	}

}

void displayBoard(int board[][3])
{
	//this will be used to place the question marks in the display board

}

void instructions ()
{
	//this will display the welcome and instructions
	cout << "**********************************************************";
	cout << "                 MISSING NUMBERS GAME                     ";
	cout << "                  A fun brain game...                  \n ";
	cout << " Please enter a whole number to guess the missing number  ";
	cout << "         Program Developed by:           ";
	cout << "**********************************************************";
}
is anyone able to explain to me why line 22 and 23 give off the identifier is undefined

It would have been helpful if you posted the complete error messages. Those messages have important information embedded within them that aid in finding and fixing the errors.

In this case where have you defined variables named playedBoard and board?

By the way your function prototypes would be better if they were before main() in the global scope.


jlb, these are the errors.
1>------ Rebuild All started: Project: Project6, Configuration: Debug Win32 ------
1>NumberGame.cpp
1>c:\users\a\source\repos\project6\project6\numbergame.cpp(22): error C2065: 'playedBoard': undeclared identifier
1>c:\users\a\source\repos\project6\project6\numbergame.cpp(23): error C2065: 'board': undeclared identifier
1>c:\users\a\source\repos\project6\project6\numbergame.cpp(75): warning C4805: '==': unsafe mix of type 'int' and type 'bool' in operation
1>Done building project "Project6.vcxproj" -- FAILED.
========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========

im still learning to do this properly,
Try putting your function declarations above int main in the global scope, as already suggested. Basically just put beginGame(); testWinner(); displayBoard(); instructions(); before/above the int main() function.
move main() so that it's the last function in the file. Or have just the prototype, aka declaration of the function, above main() , and the actual implementation below it:

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
#include <iostream>

int MyFunctionLooksLikeThis(int someparam[], size_t arr_size);

int main()
{
    // The parameter I want to pass into the function; should look like someparam above ^^
    int myarray[] = { 1, 2, 3, 4, 5 };

    // Calling the function
    int result = MyFunctionLooksLikeThis(myarray, sizeof(myarray)/sizeof(int));

    return 0;
}

// Actual function implementation
int MyFunctionLooksLikeThis(int someparam[], size_t arr_size)
{
    int max = -1;
    for (int i=0; i<arr_size; ++i)
    {
        if (someparam[i] > max)
            max = someparam[i];
    }
    return max;
}
Last edited on
i ended up moving the prototyping out the main but the errors still exist. I have no idea what Im supposed to define since its the calling of the functions that gives off the errors.

I cant delete what's inside the parentheses because then i get an error about the taking no arguments.
Last edited on
closed account (SECMoG1T)
'playedBoard': undeclared identifier
; the compiler can't find the declarataion of a variable called playedBoard . @Jlb pointed that out.
solution: just define the variables before you [pass them as arguments to function.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//functions
int beginGame(int playedBoard[]);
void displayBoard(int board[][3]);
bool testWinner(int ans, int boardNum, int ansBoard);
void instructions();

int main()
{
        const int SIZE = 3;
        int playedBoard[SIZE][SIZE]{};///how to declare a variable

	instructions();
	beginGame(playedBoard);///pass the variable as an argument.
	displayBoard(playedBoard);
///omitted code. 


warning C4805: '==': unsafe mix of type 'int' boardNum[2] == true

boardNum is of type int while true is a bool , this is might lead to some weird bugs.
maybe what you intended was: boardNum[2] == ans;
please review your func definition.
Lines 15 - 19 should be moved outside of main(), doing this will not solve the problem but function prototypes should be in the global scope.


Your problems are on lines 22 and 23 as shown in your error messages. The actual problem is that you haven't declared any variables with the names shown in the error messages.

1>------ Rebuild All started: Project: Project6, Configuration: Debug Win32 ------
1>NumberGame.cpp
1>c:\users\a\source\repos\project6\project6\numbergame.cpp(22): error C2065: 'playedBoard': undeclared identifier
1>c:\users\a\source\repos\project6\project6\numbergame.cpp(23): error C2065: 'board': undeclared identifier
1>c:\users\a\source\repos\project6\project6\numbergame.cpp(75): warning C4805: '==': unsafe mix of type 'int' and type 'bool' in operation


The last message is telling you that you're mixing an int and a bool in that if statement on line 75.

Notice the numbers that I highlighted are the line numbers where the problems are detected (some of that important information I talked about in my first post), the other important information is the name of the variables that are undeclared (note the highlighting again).

You need to declare those variables before you try to use them.

closed account (SECMoG1T)
i have also noticed some of your functions aren't very clear as to what they are supposed to be doing.

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
///it is not clear what this function does
int beginGame(int playedBoard[]) ///WARNING: unused variable playedBoard
{
	//constants for the number of rows and columns
	//understood this part
	const int row_size = 4;  ///advise: you could pass this as arguments
	const int col_size = 3;

	int boardNum1[row_size][col_size] = { { 90, 9 , 45 }, ///unused variable
	{ 66, 12, 48 },
	{ 34, 7 , 70 },
	{ 44, NULL , 26 } };

	int boardNum2[row_size][col_size] = { { 28, 10, 55 },///unused variable
	{ 89, 17, 98 },
	{ 22, 4 , 31 },
	{ 69, NULL , 78 } };

	int boardNum3[row_size][col_size] = { { 38, 11, 83 },///unused variable
	{ 15, 6 , 33 },
	{ 11, 2 , 20 },
	{ 86, NULL , 95 } };

	int ansBoard[3] = { 8, 15, 14 }; ///unused variable
	int usedBoards[3] = { NULL };     ///unused variable

	//this will allow a random board to be genterated on the board
	int maxValue = 3;
	int minValue = 1;
	int random_board_number = (rand() % (maxValue - minValue + 1)) + minValue;
	bool testWinner(random_board_number); ///ERROR: you are declaring a function here
       ///this funct returns an int
}


please provide additional info about such a funct ,so we can give some pointers, like
 "what is the function trying to achieve?" 
Last edited on
I tried declaring them and its still another error saying that its incompatible with the int. Not sure how to proceed since everything I tried leads to more errors. Im cutting my losses
Please show the current code.
closed account (SECMoG1T)
1
2
3
4
5
6
7
8
9
10
if (boardNum[0] && boardNum[1] && boardNum[2] == true)///this condition seems very suspicious
{
       cout << "You are the winner!" << endl;
}

if ((boardNum[0] == ans) && (boardNum[1] == ans) && (boardNum[2] == ans))/// is this what you intended.
{
	cout << "You are the winner!" << endl;
}


it would be better if you provide your current code as suggested by @Keskiverto also it would be better if you could provide additional info on what the functions are trying to achieve that way we would be running on the same page.
Last edited on
keskiverto wrote:
Please show the current code.

very much this
newbstarter wrote:
Not sure how to proceed since everything I tried leads to more errors. Im cutting my losses

Well, hopefully you've learned a hard lesson in building up a program incrementally, compiling along the way so that it's stable before every new feature you want to add. Don't write a novel and pray it'll work; write a complete chapter at a time.

Still, don't get discouraged -- there's bound to be a logical reason for compilation errors. Even if you can't spot it because compiler errors can sometimes be vague, someone here might be able to instantly diagnose what's wrong. This goes back to what keskiverto said... ;D
Hello newbstarter,

I made the changes that have been suggested, same thing I would have done, and the error on lines 22 and 23 went away.

Then I had a problem with line 66. This is a prototype not a function call and you already have a prototype at the beginning of the program. Then I found that what should be the function did not match the prototype and the prototype did not match the function definition. All three of these have to match with respect to theio return values and parameters.

I have not searched through the whole program to figure out what they all should be yet.

Hope that helps,

Andy
Topic archived. No new replies allowed.