C++ class, homework help

Pages: 12
#include <iostream>
#include <iomanip>
#include <cstdlib>

using namespace std;

int beginGame(int);
void displayBoard(int);
bool testWinner(int, int, int);
void instructions();

int main()
{
int board_1[4][3] = { 38, 11, 83,
15, 6, 33,
10, 2, 20,
86, NULL, 95};

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

int board_3[4][3] = { 90, 9, 45,
66, 12, 48,
34, 7, 70,
44, NULL, 26};

int ansBoard[3] = { 14,15,8 };
int playedBoards[3] = {NULL, NULL, NULL};
int userInput, number_wins = 0, guesses = 0, boardNum, ans;
bool correct, int board[4][3];
instructions();
beginGame(playedBoards[3]);
do
{
displayBoard(board[4][3]);
cout << endl << "Enter your guess or zero to exit";
cin >> userInput;
if (userInput == 0){
cout << endl << "Exiting..";
return 0;
}
else{
while (userInput < 0)
{
cout << endl << "Please enter a valid value";
cin >> userInput;
}
}

testWinner(ans, boardNum, ansBoard[3]);
if (correct == true)
{
switch (boardNum)
{
case 0:
number_wins++;
playedBoards[0] = 1 ;
}

}

else if (correct == true && ((playedBoards[0] != NULL) || (playedBoards[1] != NULL) || (playedBoards[2] != NULL)))

{
cout << endl << "Do you want to play again or exit (0)";
cin >> userInput;
if (userInput == 0){
cout << endl << "Exiting..";
return 0;
}

else{

while (userInput < 0)
{
cout << endl << "Please enter a valid value";
cin >> userInput;
}

}

if (userInput != 0)
{
beginGame(playedBoards[3]);
}

}
else
{
guesses++;
if (guesses > 3)
{
cout << endl << "Do you want to play again?. 1 to play again, 0 to exit";
cin >> userInput;
if (userInput == 0){
cout << endl << "Exiting..";
return 0;
}

else{

while (userInput < 0)
{
cout << endl << "Please enter a valid value";
cin >> userInput;
}

}

if (userInput != 0)
{
beginGame(playedBoards[3]);
}
}
}
}while (playedBoards[0] != NULL || playedBoards[1] != NULL || playedBoards[2] != NULL);
cout << endl << "You are the number guessing Champion !!";
return 0;
}



void instructions()
{
cout << "**********************************" << endl;
cout << endl << "MISSING NUMBERS GAME" << endl;
cout << "A Fun Brain Game" << endl << endl << endl;
cout << "Please enter a whole number to guess the missing number...";
cout << endl << "Program Developed by: Rohan Jakhete" << endl;
cout << "**********************************" << endl;
}

int beginGame(int playedBoards[3])
{
int randomNum;
randomNum = (rand() % (3));

switch (randomNum)
{
case 0:
do
{
randomNum = (rand() % (3));
} while (playedBoards[randomNum] != NULL);
return randomNum;

case 1:
do {
randomNum = (rand() % (3));
} while (playedBoards[randomNum] != NULL);
return randomNum;

case 2:
do {
randomNum = (rand() % (3));
} while (playedBoards[randomNum] != NULL);
return randomNum;

}
}

void displayBoard(int board[][3])
{
int randomNum;
int board_1[4][3], board_2[4][3], board_3[4][3];
switch (randomNum)
{
case 0:
for (int x = 0; x < 4; x++)
{
for (int y = 0; y < 3; y++)
{
cout << setw(4) << board_1[4][3] << " ";
}
cout << endl;
}
case 1:
for (int x = 0; x < 4; x++)
{
for (int y = 0; y < 3; y++)
{
cout << setw(4) << board_2[4][3] << " ";
}
cout << endl;
}
case 2:
for (int x = 0; x < 4; x++)
{
for (int y = 0; y < 3; y++)
{
cout << setw(4) << board_3[4][3] << " ";
}
cout << endl;
}
}

}


bool testWinner(int ans, int boardNum, int ansBoard[])
{
int userInput;
if (userInput == ansBoard[boardNum])
{
cout << endl << "Nice Job!!";
return true;
}
else
{
cout << endl << "Sorry, that is incorrect";
return false;
}

}
if anybody has time to look through this, please help me. I am a 10th grader in high school and this is my homework assignment. I am getting a lot of errors
> I am getting a lot of errors

What are those errors? (Post the full text of the error diagnostic.)
Place your code within code tags. See: http://www.cplusplus.com/articles/jEywvCM9/
Most of errors are undefined reference to the functions ??
for example, undefined reference to beginGame
1. As JLBorges said, please use code tags to make your code readable.

2. You need to make your function declarations, definitions and calls consistent. You declare a function beginGame that will take a single integer:

int beginGame(int);

However, when you define the function, you define it to take an array of integers:

1
2
3
4
int beginGame(int playedBoards[3])
{
  //...
}


When you call it, you're trying to call it as if it takes a single integer:

beginGame(playedBoards[3]);

Note also that array indices start at 0, not one, so playedBoards[3] would be the 4th element of the array, not the third element. However, you've only defined that array to have 3 elements, so you're actually attempting to read memory past the end of the array.

I suspect what you actually wanted to do was pass the whole array into the function, in which case you just pass the name of the array, without any index.
Last edited on
line 34 - correct; instead of correct,
bool correct, int board[4][3];

The code in each of the switch cases below is identical. What is this function trying to do?

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
int beginGame(int playedBoards[3])
{
    int randomNum;
    randomNum = (rand() % (3));
    
    switch (randomNum)
    {
        case 0:
                do
                {
                    randomNum = (rand() % (3));
                } while (playedBoards[randomNum] != NULL);
                return randomNum;
        
        case 1:
                do {
                    randomNum = (rand() % (3));
                } while (playedBoards[randomNum] != NULL);
                return randomNum;
        
        case 2:
                do {
                    randomNum = (rand() % (3));
                } while (playedBoards[randomNum] != NULL);
                return randomNum; 
    }
}


A variable randomNum is created here, but has no value when the switch tries to evaluate it.

There are no break statements between the individual cases in the switch statement.

You create new board_1, board_2 and board_3 arrays in this function. Do you mean to print out the values in those same named arrays that you created in main?

board_1[4][3] would be out of bounds of the array, I think you mean to use board_1[x][y].

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
void displayBoard(int board[][3])
{
    int randomNum;
    int board_1[4][3], board_2[4][3], board_3[4][3];
    switch (randomNum)
    {
        case 0:
                for (int x = 0; x < 4; x++)
                {
                    for (int y = 0; y < 3; y++)
                    {
                        cout << setw(4) << board_1[4][3] << " ";
                    }
                    cout << endl;
                }
        case 1:
                for (int x = 0; x < 4; x++)
                {
                    for (int y = 0; y < 3; y++)
                    {
                        cout << setw(4) << board_2[4][3] << " ";
                    }
                    cout << endl;
                }
        case 2:
            for (int x = 0; x < 4; x++)
            {
                for (int y = 0; y < 3; y++)
                {
                    cout << setw(4) << board_3[4][3] << " ";
                }
                cout << endl;
            }
    }
}


A variable userInput is created, but has no value when the code tries to compare it in the if statement. Should it be comparing the ans variable that is sent in to the function?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
bool testWinner(int ans, int boardNum, int ansBoard[])
{
    int userInput;
    if (userInput == ansBoard[boardNum])
    {
        cout << endl << "Nice Job!!";
        return true;
    }
    else
    {
        cout << endl << "Sorry, that is incorrect";
        return false;
    }
}
Last edited on
Thank for all of your input, but I am still having issues. But, I have a questions about the variable, userInput. I initialized it in the main function and it was assigned a value in another function. Will that value retain for other 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
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#include <iostream>
#include <iomanip>
#include <cstdlib>

using namespace std;

int beginGame(int[]);
void displayBoard(int[][3]);
bool testWinner(int, int, int[]);
void instructions();

int main()
{
    int board_1[4][3] = { 38, 11, 83,
                         15, 6, 33,
                         10, 2, 20,
                         86, NULL, 95};

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

	int board_3[4][3] = { 90, 9, 45,
                         66, 12, 48,
                         34, 7, 70,
                         44, NULL, 26};

	int ansBoard[3] = { 14,15,8 };
	int playedBoards[3] = {NULL, NULL, NULL};
	int userInput, number_wins = 0, guesses = 0, boardNum, ans;
    bool correct; int board[4][3];
    instructions();
    beginGame(playedBoards[3]);
    do
    {
        displayBoard(board[4][3]);
        cout << endl << "Enter your guess or zero to exit";
        cin >> userInput;
        if (userInput == 0){
            cout << endl << "Exiting..";
            return 0;
        }
        else{
        while (userInput < 0)
        {
            cout << endl << "Please enter a valid value";
            cin >> userInput;
        }
        }

            testWinner(ans, boardNum, ansBoard[3]);
            if (correct == true)
            {
				switch (boardNum)
				{
				case 0:
					number_wins++;
					playedBoards[0] =  1 ;
			   }

            }

            else if (correct == true && ((playedBoards[0] != NULL) || (playedBoards[1] != NULL) || (playedBoards[2] != NULL)))

            {
                cout << endl << "Do you want to play again or exit (0)";
                cin >> userInput;
                if (userInput == 0){
                    cout << endl << "Exiting..";
                    return 0;
                }

                else{

                while (userInput < 0)
                {
                    cout << endl << "Please enter a valid value";
                    cin >> userInput;
                }

                }

                if (userInput != 0)
                {
                    beginGame(playedBoards[3]);
                }

            }
            else
            {
                guesses++;
                if (guesses > 3)
                {
                    cout << endl << "Do you want to play again?. 1 to play again, 0 to exit";
                    cin >> userInput;
                    if (userInput == 0){
                    cout << endl << "Exiting..";
                    return 0;
                    }

                    else{

                    while (userInput < 0)
                    {
                    cout << endl << "Please enter a valid value";
                    cin >> userInput;
                    }

                    }

                    if (userInput != 0)
                    {
                        beginGame(playedBoards[3]);
                    }
                }
            }
    }while (playedBoards[0] != NULL || playedBoards[1] != NULL || playedBoards[2] != NULL);
        cout << endl << "You are the number guessing Champion !!";
		return 0;
}



void instructions()
{
	cout << "**********************************" << endl;
	cout << endl << "MISSING NUMBERS GAME" << endl;
	cout << "A Fun Brain Game" << endl << endl << endl;
	cout << "Please enter a whole number to guess the missing number...";
	cout << endl << "Program Developed by: Rohan Jakhete" << endl;
	cout << "**********************************" << endl;
}

int beginGame(int playedBoards[])
{
	int randomNum;
	randomNum = (rand() % (3));

	switch (randomNum)
	{
	case 0:
		do
        {
          randomNum = (rand() % (3));
		} while (playedBoards[0] != NULL);
		return randomNum;
		break; 

	case 1:
		do {
			randomNum = (rand() % (3));
		} while (playedBoards[1] != NULL);
		return randomNum;
		break; 
	case 2:
		do {
			randomNum = (rand() % (3));
		} while (playedBoards[2] != NULL);
		return randomNum;

    }
}

void displayBoard(int board[][3])
{
    int randomNum;
    int board_1[4][3], board_2[4][3], board_3[4][3];
    switch (randomNum)
    {
    case 0:
        for (int x = 0; x < 4; x++)
    {
        for (int y = 0; y < 3; y++)
        {
            cout << setw(4) << board_1[x][y] << " ";
        }
        cout << endl;
    }
    case 1:
        for (int x = 0; x < 4; x++)
    {
        for (int y = 0; y < 3; y++)
        {
            cout << setw(4) << board_2[x][y] << " ";
        }
        cout << endl;
    }
    case 2:
        for (int x = 0; x < 4; x++)
    {
        for (int y = 0; y < 3; y++)
        {
            cout << setw(4) << board_3[x][y] << " ";
        }
        cout << endl;
    }
    }

}


bool testWinner(int ans, int boardNum, int ansBoard[])
{
    int userInput;
    if (userInput == ansBoard[boardNum])
    {
        cout << endl << "Nice Job!!";
        return true;
    }
    else
    {
        cout << endl << "Sorry, that is incorrect";
        return false;
    }

}
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
||=== Build file: "no target" in "no project" (compiler: unknown) ===|
C:\Users\rohan\Desktop\Source.cpp||In function 'int main()':|
C:\Users\rohan\Desktop\Source.cpp|17|warning: converting to non-pointer type 'int' from NULL [-Wconversion-null]|
C:\Users\rohan\Desktop\Source.cpp|22|warning: converting to non-pointer type 'int' from NULL [-Wconversion-null]|
C:\Users\rohan\Desktop\Source.cpp|27|warning: converting to non-pointer type 'int' from NULL [-Wconversion-null]|
C:\Users\rohan\Desktop\Source.cpp|30|warning: converting to non-pointer type 'int' from NULL [-Wconversion-null]|
C:\Users\rohan\Desktop\Source.cpp|30|warning: converting to non-pointer type 'int' from NULL [-Wconversion-null]|
C:\Users\rohan\Desktop\Source.cpp|30|warning: converting to non-pointer type 'int' from NULL [-Wconversion-null]|
C:\Users\rohan\Desktop\Source.cpp|32|error: expected unqualified-id before 'int'|
C:\Users\rohan\Desktop\Source.cpp|34|error: invalid conversion from 'int' to 'int*' [-fpermissive]|
C:\Users\rohan\Desktop\Source.cpp|7|note: initializing argument 1 of 'int beginGame(int*)'|
C:\Users\rohan\Desktop\Source.cpp|37|error: 'board' was not declared in this scope|
C:\Users\rohan\Desktop\Source.cpp|52|error: invalid conversion from 'int' to 'int*' [-fpermissive]|
C:\Users\rohan\Desktop\Source.cpp|9|note: initializing argument 3 of 'bool testWinner(int, int, int*)'|
C:\Users\rohan\Desktop\Source.cpp|64|warning: NULL used in arithmetic [-Wpointer-arith]|
C:\Users\rohan\Desktop\Source.cpp|64|warning: NULL used in arithmetic [-Wpointer-arith]|
C:\Users\rohan\Desktop\Source.cpp|64|warning: NULL used in arithmetic [-Wpointer-arith]|
C:\Users\rohan\Desktop\Source.cpp|86|error: invalid conversion from 'int' to 'int*' [-fpermissive]|
C:\Users\rohan\Desktop\Source.cpp|7|note: initializing argument 1 of 'int beginGame(int*)'|
C:\Users\rohan\Desktop\Source.cpp|114|error: invalid conversion from 'int' to 'int*' [-fpermissive]|
C:\Users\rohan\Desktop\Source.cpp|7|note: initializing argument 1 of 'int beginGame(int*)'|
C:\Users\rohan\Desktop\Source.cpp|118|warning: NULL used in arithmetic [-Wpointer-arith]|
C:\Users\rohan\Desktop\Source.cpp|118|warning: NULL used in arithmetic [-Wpointer-arith]|
C:\Users\rohan\Desktop\Source.cpp|118|warning: NULL used in arithmetic [-Wpointer-arith]|
C:\Users\rohan\Desktop\Source.cpp||In function 'int beginGame(int*)':|
C:\Users\rohan\Desktop\Source.cpp|146|warning: NULL used in arithmetic [-Wpointer-arith]|
C:\Users\rohan\Desktop\Source.cpp|152|warning: NULL used in arithmetic [-Wpointer-arith]|
C:\Users\rohan\Desktop\Source.cpp|158|warning: NULL used in arithmetic [-Wpointer-arith]|
||=== Build failed: 6 error(s), 15 warning(s) (0 minute(s), 0 second(s)) ===|



These are all the errors. Might be a little difficult to read. Please help if possible
> warning: converting to non-pointer type 'int' from NULL
> warning: NULL used in arithmetic
use 0 instead of NULL
(you are storing numbers)

> 32|error: expected unqualified-id before 'int'
> 37|error: 'board' was not declared in this scope
not having those errors
but on your old code you had bool correct, int board[4][3]; where that comma (,) after `correct' should have been a semicolon (;)
perhaps you are not compilling your current code but an old version.


> 34|error: invalid conversion from 'int' to 'int*'
To pass the array, use its name beginGame(playedBoards);
you've got this error in several places
1
2
displayBoard(board);
testWinner(ans, boardNum, ansBoard);



> But, I have a questions about the variable, userInput. I initialized it in
> the main function and it was assigned a value in another function. Will that
> value retain for other functions?
checkout http://www.cplusplus.com/doc/tutorial/namespaces/ (scopes)
`userInput' in main() and `userInput' in testWinner() are two different variables that have no relationship between them.
So `userInput' remains uninitialized in testWinner()
Ok, I fixed all the issues. When I run the program, the only function that runs is instructions(); Then, it asks for user input and returns a weird value.
1
2
3
4
            cin >> userInput;

            testWinner(ans, boardNum, ansBoard[3]);
            if (correct == true)


Should userInput be sent into the testWinner function instead of ans? Then the testWinner function can check the value of ans against the board?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
bool testWinner(int ans, int boardNum, int ansBoard[])
{
    if (ans == ansBoard[boardNum])
    {
        cout << endl << "Nice Job!!";
        return true;
    }
    else
    {
        cout << endl << "Sorry, that is incorrect";
        return false;
    }

}



Where should boardNum come from? Should this be the value returned from the beginGame function?
e.g. boardNum = beginGame(playedBoards);

Likewise, where should the value for correct from? Is that the bool value returned from the testWinner function? e.g. correct = testWinner(ans, boardNum, ansBoard);
Last edited on
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>

using namespace std;

int beginGame(int[]);
void displayBoard(int[][3]);
bool testWinner(int &, int, int[]);
void instructions();

int main()
{
    int board_1[4][3] = { 38, 11, 83,
                         15, 6, 33,
                         10, 2, 20,
                         86, NULL, 95};

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

	int board_3[4][3] = { 90, 9, 45,
                         66, 12, 48,
                         34, 7, 70,
                         44, NULL, 26};

	int ansArray[3] = { 14,15,8 };
	int usedBoards[3] = {NULL};
	int userInput, number_wins = 0, guesses = 0, boardNumber, ans, randomNum;
    bool correct; int main_board[4][3];
    instructions();
    beginGame(usedBoards);
    do
    {
        displayBoard(main_board);
        cout << endl << "Enter your guess or zero to exit";
        cin >> userInput;
        if (userInput == 0){
            cout << endl << "Exiting..";
            return 0;
        }
        else{
        while (userInput < 0)
        {
            cout << endl << "Please enter a valid value";
            cin >> userInput;
        }
        }

            testWinner(userInput, boardNumber, ansArray);
            if (correct == true)
            {
				switch (boardNumber)
				{
				case 0:
					number_wins++;
					usedBoards[0] =  1 ;
			   }

            }

            else if (correct == true && ((usedBoards[0] != NULL) || (usedBoards[1] != NULL) || (usedBoards[2] != NULL)))

            {
                cout << endl << "Do you want to play again or exit (0)";
                cin >> userInput;
                if (userInput == 0){
                    cout << endl << "Exiting..";
                    return 0;
                }

                else{

                while (userInput < 0)
                {
                    cout << endl << "Please enter a valid value";
                    cin >> userInput;
                }

                }

                if (userInput != 0)
                {
                    beginGame(usedBoards);
                }

            }
            else
            {
                guesses++;
                if (guesses > 3)
                {
                    cout << endl << "Do you want to play again?. 1 to play again, 0 to exit";
                    cin >> userInput;
                    if (userInput == 0){
                    cout << endl << "Exiting..";
                    return 0;
                    }

                    else{

                    while (userInput < 0)
                    {
                    cout << endl << "Please enter a valid value";
                    cin >> userInput;
                    }

                    }

                    if (userInput != 0)
                    {
                        beginGame(usedBoards);
                    }
                }
            }
    }while (usedBoards[0] != NULL || usedBoards[1] != NULL || usedBoards[2] != NULL);
        cout << endl << "You are the number guessing Champion !!";
		return 0;
}



void instructions()
{
	cout << "**********************************" << endl;
	cout << endl << "MISSING NUMBERS GAME" << endl;
	cout << "A Fun Brain Game" << endl << endl << endl;
	cout << "Please enter a whole number to guess the missing number...";
	cout << endl << "Program Developed by: Rohan Jakhete" << endl;
	cout << "**********************************" << endl;
}

int beginGame(int playedBoards[])
{
	int randomNum;
	int boardNumber;
	srand(time(0));
	randomNum = (rand() % (3));

	switch (randomNum)
	{
	case 0:
		do
        {
          randomNum = (rand() % (3));
		} while (playedBoards[0] != NULL);
		return randomNum;
		randomNum = boardNumber;
		break;

	case 1:
		do {
			randomNum = (rand() % (3));
		} while (playedBoards[1] != NULL);
		return randomNum;
		randomNum = boardNumber;
		break;
	case 2:
		do {
			randomNum = (rand() % (3));
		} while (playedBoards[2] != NULL);
		return randomNum;
		randomNum = boardNumber;

    }
}

void displayBoard(int board[][3])
{
    int randomNum;
    int board_1[4][3], board_2[4][3], board_3[4][3];
    switch (randomNum)
    {
    case 0:
        for (int x = 0; x < 4; x++)
    {
        for (int y = 0; y < 3; y++)
        {
            cout << setw(4) << board_1[x][y] << " ";
        }
        cout << endl;
    }
    case 1:
        for (int x = 0; x < 4; x++)
    {
        for (int y = 0; y < 3; y++)
        {
            cout << setw(4) << board_2[x][y] << " ";
        }
        cout << endl;
    }
    case 2:
        for (int x = 0; x < 4; x++)
    {
        for (int y = 0; y < 3; y++)
        {
            cout << setw(4) << board_3[x][y] << " ";
        }
        cout << endl;
    }
    }

}


bool testWinner(int &ans, int boardNum, int ansBoard[])
{
    int userInput;
    if (userInput == ansBoard[boardNum])
    {
        cout << endl << "Nice Job!!";
        return true;
    }
    else
    {
        cout << endl << "Sorry, that is incorrect";
        return false;
    }

}


Here is the updated code. Although, all the functions have to be named exactly how they are right now. Thank you in advance.
For the userInput variable, I am not allowed to make it global variables, but if I declare it in the main function and receive input in the main function, then will the other functions also receive the value of userInput? Or do I have to make it a reference variable?
When I run the program, the only function that runs is instructions(); Then, it asks for user input and returns a weird value. It does not display the game board. Please help with this as well.

Just want to say, everybody on this forum is very helpful. Thank you for all your help in advance.
Just want to double check how this game is supposed to run. The person gets shown one of the three game boards at random, and they have three chances to guess the missing number? If they guess the number or run out of guesses, they can choose to play again with one of the remaining boards? If all boards have been played, the game ends?
Yes, exactly!
Just some ideas, others might have some other suggestions. Saving the boardNumber returned from the beginGame function, the main function can then call displayBoard with the appropriate board. I take it you have to have these three particular fixed boards, not something that could be dynamically generated?

1
2
3
4
5
6
7
8
9
10
11
12
        boardNumber = beginGame(usedBoards); 
         switch(boardNumber){
            case 0:
                   displayBoard(board_1);
                   break;
            case 1:
                   displayBoard(board_2);
                   break;
            case 2:
                   displayBoard(board_3);
                   break;
        }


I haven't tested all of this, but it seems you could simplify the functions.

1
2
3
4
5
6
7
8
9
10
int beginGame(int playedBoards[])
{
    int randomNum;
    while (true){
        randomNum = rand() % 3;
        if (playedBoards[randomNum] != 1)// if not already played
            break;
    }
    return randomNum;
}


1
2
3
4
5
6
7
8
9
10
11
void displayBoard(int board[][3])
{
    for (int x = 0; x < 4; x++)
    {
        for (int y = 0; y < 3; y++)
        {
            cout << setw(4) << board[x][y] << " ";
        }
        cout << endl;
    }
}


1
2
3
4
5
6
7
8
9
10
11
12
13
bool testWinner(int ans, int boardNum, int ansBoard[])
{
    if (ans == ansBoard[boardNum])
    {
        cout << endl << "Nice Job!!";
        return true;
    }
    else
    {
        cout << endl << "Sorry, that is incorrect";
        return false;
    }
}


(in main)
correct = testWinner(userInput, boardNumber, ansArray);
Last edited on
Thank you, I followed your advice, but this is my console window???

**********************************

MISSING NUMBERS GAME
A Fun Brain Game


Please enter a whole number to guess the missing number...
Program Developed by: Rohan Jakhete
**********************************
90 9 45
66 12 48
34 7 70
44 0 26
1990075172 7012076 1989762920
8 1989700323 1989700314
-113801443 4199040 4199040
0 4311472 7012032

Enter your guess or zero to exit8

Nice Job!!
Nice Job!!
You are the number guessing Champion !!
Process returned 0 (0x0) execution time : 4.014 s
Press any key to continue.



Pages: 12