Object Oriented Number Guessing Game with Range

Hi could someone help me with my existing code !? I need Write an OBJECT-ORIENTED program using
2 Classes
to create a guessing game in which the user must guess a number between
1 and 100
. The user is given a
maximum of 10
tries to guess each number. He can play as many rounds as he wants, each time with a new number to guess. Begin with a screen describing the game and asking for the player’s name. - If incorrect, display "Too High" or "Too Low". Adjust the
number of guesses left
and
the current range
:

This is what the program suppose to do :


What’s your name? John Doe

[Round 1 -- assume number is 85]

You have 10 guesses
Guess a number between 1 and 100: 50

Too low
You have 9 guesses
Guess a number between 51 and 100: 72

Too low
You have 8 guesses
Guess a number between 73 and 100: 90

Too high
You have 7 guesses
Guess a number between 73 and 89: 95

Hey, pay attention! I won’t count that one.
Guess a number between 73 and 89: 85
You got it!!

Do you want to play again (y/n)? Y

[Round 2 -- assume number is 25]

You have 10 guesses
Guess a number between 1 and 100: 50

Too high
You have 9 guesses
Guess a number between 1 and 49: […etc.]

[…user keeps guessing wrong…]

You have 1 guess
Guess a number between 23 and 27: 26

Sorry, you lose…the number was 25

Do you want to play again (y/n)? Y

[Round 3 -- assume number is 99]

You have 10 guesses
Guess a number between 1 and 100: 99
You got it!!

Do you want to play again (y/n)? N

Hey, John Doe, you won 2 out of 3 rounds.
That’s 67% - don’t quit your day job.
For the numbers you guessed correctly, you took an average of 2.5 guesses
You join Kreskin’s psychic club for guessing one on the first guess!






Problem : I am not sure if I should fill my array with number or fill it with "False" values? Everytime I run the program it will display " number found" on each guess

--------------------------------------------------
Here is my current 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
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
#include <iostream>
#include <iomanip>
#include <string>
#include <conio.h>
#include <ctime>
using namespace std;


class Scoreboard
{
	private:
		string player_name;													// name of player
		int round;															// number of rounds
		double guess_avg;													// average of guesses
		double tot_score;													// total score

	public:
		Scoreboard()														// default constructor
		{
			player_name ="";
			round = 0;
			guess_avg = 0.00;
			tot_score = 0.00;
		}
		void input_name();													// prototype
		string get_name()	{	return player_name;		}					// get player's name
		int getRound()		{	return round;			}					// number of round played
		double	cal_avg()	{	return guess_avg;		}					// average of player
};

class Game
{
	private:
		int numb_array[100];												// array of 100 numbers (0 to 99)
		int	guess_numb;														// temp number entered
		int win_numb;														// wining number
		int elct;
		int found;															// *** BINARY SEARCH ***
		int low;															// lowest subscript to consider
		int high;															// highest subscript to consider
		int middle;

	public:
		Game()																// default constructor
		{
			for (int i = 0; i < 100; i++)
			{
				numb_array[i] = false;										// initialize flag array
			}						
			guess_numb;														// guess number
			win_numb;														// winning number
			srand(time(NULL));												// seed random no's

			int elct = 100;
			int found = false;													// *** BINARY SEARCH ***
			int low = 0;													// lowest subscript to consider
			int high = elct -1;												// highest subscript to consider
			int middle = 0;
		}

		void input_numb();
		void pick_number();
		int search_numb(int guess_numb);
		int generate_numb()	{	return rand()% 100 + 1;		}
		int get_guess_numb(){	return guess_numb;			}
		int get_win_numb()	{	return win_numb;			}
		int display_array() 
		{
			for (int i = 0; i < 100; i++)
			{
				cout << numb_array[i];
			}
			return 0;
		}
};

void Scoreboard::input_name()
{
	cout << "Enter player's name: ";
	getline(cin, player_name);
}

void Game::pick_number()
{
	win_numb = generate_numb();
	numb_array[win_numb] = win_numb;


}
int Game::search_numb(int guess_numb)
{


	while (found == false && low <= high)
	{
		middle = (low + high) / 2;				// determine midpoint

		if ( numb_array[middle] == guess_numb )
			found = true;					// found it
		else if ( guess_numb < numb_array[middle])	
			high = middle - 1;					// cut search range in half
		else
			low = middle + 1;					// cut search range in half
	}
	return found;
}

void Game::input_numb()
{
	int found = -1;	
	for (int i = 10; i > 0 && found == -1; i--)
	{
		int temp;

		cout << "You have " << i << " guesses" << endl;
		cout << "Guess a number: ";
		cin >>	guess_numb;
		temp = search_numb(guess_numb); 
		if (temp != false)
			cout << "The number is found!";
	}
	
}

char display_menu()						
{
	char choice;
																			// display menu
	cout << endl << endl << "   MAIN MENU" << endl << endl;	
	cout << "A. Play a new game" << endl;	
	cout << "X. Exit" << endl;
	cout << "Make a choice: ";
	cin >> choice;															// get & return user choice
	return choice;
}

int main()
{

	char choice;
	Scoreboard myScore;
	Game arr;
	myScore.input_name();

	do 
		{
			Game myGame;				
			choice = toupper(display_menu());								// call function to display menu

			switch (choice)				
			{
				case 'A':													// select procedure to follow
					myGame.pick_number();									// generate a random number and place it in array
					myGame.input_numb();									// prompts for a guess number
					break;
				case 'X':													// do nothing if Exit chosen
					cout << "The winning number is " <<  myGame.get_win_numb() << endl;
					break;
				default:
					cout << endl << "Invalid choice -- try again" << endl;
			}
		} while (choice != 'X');											// continue until Exit chosen


	cout << "Hey, "<< myScore.get_name() << " it is working!!" << endl;
	arr.display_array();
	//myGame.pick_number();
	//myGame.search_numb();
	system("Pause");
	return 0;
}



Last edited on
no need just use a static bool or a just bool if your only running one instance at a time.
Topic archived. No new replies allowed.