Hi could someone help me with my existing code !? I need Write an OO program using two Classes
to create a guessing game in which the user must guess a number between 1 and 100
. The user is given a maximum try of 10
tries to guess each number. they 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 it 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 did it by guessing it right on the first guess!


Problem : I am not sure if I did it good as everytime I run program it will display " The number is found! You have 9 guesses left" on each guess

--------------------------------------------------
Here is my current code :

#include <iostream>
#include <iomanip>
#include <string>
#include <conio.h>
#include <ctime>
using namespace std;


class Scoreboard
{
	private:
		string player_name;													
		int round;																	double guess_avg;													
		double tot_score;													

	public:
		Scoreboard()																{
			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];												
		int	guess_numb;														
		int win_numb;													
	int elct;
		int found;															
		int low;																	int high;															
		int middle;

	public:
		Game()															
		{
			for (int i = 0; i < 100; i++)
			{
				numb_array[i] = false;										
			}						
			guess_numb;													
			win_numb;														
			srand(time(NULL));												

			int elct = 100;
			int found = false;															int low = 0;													
			int high = elct -1;												
			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;															
	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':													
					myGame.pick_number();									 number and place it in array
					myGame.input_numb();									 number
					break;
				case 'X':																		cout << "The winning number is " <<  myGame.get_win_numb() << endl;
					break;
				default:
					cout << endl << "Invalid choice -- try again" << endl;
			}
		} while (choice != 'X');											


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

tks for the aid...
George Albama

This is a classic case of ignoring the compile warnings! It is 99.999% essential to turn on compiler warnings and fix the ALL. [Yes sometimes that means turning a particular warning off, but at least you are aware of exactly what caused it]

In your case you have a constructor like this:

Game()               
    {
      for (int i = 0; i < 100; i++)
	{
	  numb_array[i] = false;          
	}      
      guess_numb;         // This does nothing      
      win_numb;           // This also does nothing
      srand(time(NULL));
      int elct = 100;
      int found = false;           // this is a LOCAL copy of found that 
      int low = 0;                 // masks the copy in the class.
      int high = elct -1;            
      int middle = 0;
  }

you have created local copies that are only in existence until the end of the function. You compiler would have warned you about that.

So a much better way to do constructors, is to initialize objects/values in an initializer list. e.g.

Game() : guess_numb(0),win_numb(0),elct(100),found(0),
     low(0),high(elct-1),middle(0)
{
   for(int i=0;i<100;i++)
     number_array[i]=0;
}

But the MAIN reason that you posted here was that you either did not activate all the warnings/ turned them off/ or ignored them. By putting warning on, you will find lots of bugs and problems much quicker. The compile is your friend, it is much much more difficult to find errors at runtime than at compile time.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.