Assistance in a code problem

Write your question here.


Hope everyone is doing well today! So, I'm doing a homework assignment, and I've gotten a lot of the project completed, but, I've come to a problem that I'm unsure how to resolve.

A little about the project, I wrote a code that is best described as a missing numbers game. The purpose of the code is to replace the 0 in the number table with the missing number and the program will output that you are correct and give you the 2nd problem to solve, and again for the 3rd. Once you've solved all 3 problems, the program will output "You are the number game champion, CONGRATULATIONS!!" The issue that I am having is, how do I isolate the cout statement in my code that asks the user "do you want to play again? Enter 0 to exit or any number to continue" to where it only asks that question for table 1 and table 2? I feel like it's something easy and I'm just overthinking this too much. Any help would be great!

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
 #include <iostream>
#include <iomanip>
#include <cstdlib>
#include <string>
#include <ctime>

using namespace std;

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

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

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

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

	int ansBoard[3] = { 14,15,8 };
	int usedBoards[3] = { 0,0,0 };
	int userInput, number_wins = 0, guesses, boardNumber;
	bool winner;
	

	instructions();

	do {
		
		guesses = 0; 
		winner = false; 
		boardNumber = beginGame(usedBoards);
											
		switch (boardNumber) { 
		case 0:
			displayBoard(board_1);
			break;
		case 1:
			displayBoard(board_2);
			break;
		case 2:
			displayBoard(board_3);
			break;
		}
		
		while (guesses < 3 && !winner) { 
			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;
				}
			}

			guesses++;
			winner = testWinner(userInput, boardNumber, ansBoard);
		}

		if (winner) {
			number_wins++;
		}
		else {
			cout << endl << "Sorry, you ran out of guesses for this board.\n";
		}
		
	} while (!(usedBoards[0] == 1 && usedBoards[1] == 1 && usedBoards[2] == 1));

	 if (number_wins == 3)
	cout << endl << "***You are the number guessing Champion!! CONGRATULATIONS!! ***" << endl;
	 cout << "Press any key to continue. . ." << endl;
	 cin.get();
	 cin.ignore();
	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: Joe Shmo" << endl;
	cout << "**********************************" << endl;
}

int beginGame(int playedBoards[])
{
	int randomNum;
	while (true) {
		randomNum = rand() % 3;
		if (playedBoards[randomNum] != 1)
			break; 
	}
	playedBoards[randomNum] = 1; 

	return randomNum; 
}

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;
	}
}


bool testWinner(int ans, int boardNum, int ansBoard[]) {

	if (ans == ansBoard[boardNum]) {
		cout << endl << "You are a number genius!!" << endl << endl;
		
		cout << "Do you want to play again?? Enter 0 to Exit or any number to continue... > " << endl << endl;
		return true;
	}
	
	else
	{
		cout << endl << "Sorry, that is incorrect";
		return false;
	}
}


Last edited on
My apologies! I figured it out!!!!
Topic archived. No new replies allowed.