Hi all;
I need help with a program, where its a game where users are to guess a randomly selected number. The user has a max of 1 - 100.

class CScoreboard
{
	private:
		string players_name;														
                   int round;															
		int guess_avg;													
		int total_score;													
	public:
		CScoreboard()																{
			players_name ="";
			round = 0;
			guess_avg = 0;
			total_score = 0;
		}
		void input_name();													
		string get_name()	
		{	
			return players_name;		
		}					
		int getRound()		
		{	
			return round;			
		}				
		double	cal_avg()	
		{	
			return guess_avg;		
		}					
};

class CGame
{
	private:
		int numb_array[100];												 
		int	guess_numb;											 
		int win_numb;														 
		int elct;
		int found;															 		int low;															
		int high;															
		int middle;

	public:
		CGame()																// default constructor
		{
			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 CScoreboard::input_name()
{
	cout << "Please enter your name: ";
	getline(cin, players_name);
	cout << endl << "Welcome to the Main Menu, " << players_name;
}

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


}
int CGame::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 CGame::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!" << endl;
	}
	
}

char display_menu()						
{
	char choice;
																			
	cout << endl << endl << "   MAIN MENU" << endl << endl;	
	cout << "P. Press 'P' to play" << endl;	
	cout << "Q. Press 'Q' to Quit" << endl;
	cout << "Please make your selection: ";
	cin >> choice;															
	return choice;
}

int main()
{
		char choice;
	CScoreboard myScore;
	CGame arr;
	myScore.input_name();

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

			switch (choice)				
			{
				case 'P':													
					myGame.pick_number();									
					myGame.input_numb();									
					break;
				case 'Q':													
		cout << "The correct number is: " <<  myGame.get_win_numb() << endl;
					break;
				default:
					cout << endl << "Invalid choice -- You have been warned!" << endl;
			}
		} while (choice != 'X');												


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

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? Joe Smoe
[Round 1 -- assume number is 90]

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

Too low
You have 9 guesses
etc,

When I run this it does 2 things incorrectly
First, if you choose to "Quit" immediately, it displays a -858993460, and second, after the user enters a number, the system displays "The number is found"
This is what I am trying to fix, and don't know where...

Here are the compiler output
1>------ Build started: Project: GuessGame, Configuration: Debug Win32 ------
1> NameThatNumber.cpp
1>c:\home\programs\NameThatNumber.cpp(67): warning C4244: 'argument' : conversion from 'time_t' to 'unsigned int', possible loss of data
1> Assignments.vcxproj -> C:\home\programs\Debug\program.exe
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========

Georgy

Make search_num return a bool, it'll make life easier. Be careful with lines like 46, just assign 0 instead. False might work, but it might not, I'm not sure.

Look at lines 54-56 for one of the major sources of error. What you have done is declare 3 variables within your constructor that mask the three declared in the private section. When your constructor completes, those variables are destroyed and the others go uninitialized. So take off the int from the variables on 54-56 and you'll be fine.

In terms of your compiler error, change your srand line to srand((unsigned)time(0)); so that you explicitly cast to the unsigned that srand wants.

I'm sure those aren't the only issues but it will get you started.

Edited 6 Years Ago by jonsca: n/a

Thanks for your help!! Can you help me with the commented section

int main()
{		
char choice;	
CScoreboard myScore;	
CGame arr;	
myScore.input_name();
// CGame myGame;

I am trying to call the functions below, within the main, and it tells me its not defined...

int main()
{
	intro();
	char choice;
	CScoreboard myScore;
	CGame arr;
	myScore.input_name();
	// CGame myGame;

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

			switch (choice)				
			{
				case 'P':													
					myGame.pick_number();		
					myGame.input_numb();	
					break;
				case 'Q':													
	cout << "The correct number is: " <<  myGame.get_win_numb() << endl;
					break;
				default:
					cout << endl << "Invalid choice -- Please 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;
}

Thanks again..Sorry if it didn't present well, as I tried using the proper tags....

Edited 6 Years Ago by georgy9002: n/a

You need to take a quick trip through your textbook on scoping.
With any set of braces (known as a block):

{
  int myvariable;

  myvariable = 42;
}

cout << myvariable;  //compiler says: who? => error

by the time you want to access myGame's methods at the end, it's already destroyed since it was in the while block.

Also, thanks for trying to use the code tags, you can probably still go back and edit this post to fix them, they have to be [code] (2 brackets)

Edited 3 Years Ago by Dani: Formatting fixed

Thanks for that! I did try to go back and re-edit this and was unable to...
Now my question is, what would happen if I add it to main

int main()
{
	intro();
	char choice;
	CScoreboard myScore;
	CGame arr;
	myScore.input_name();
	// CGame myGame;

Is that permitable?

I also would like to know, why if you choose to "Quit" immediately, it displays a -858993460

Edited 6 Years Ago by georgy9002: n/a

Yes, you should put your CGame in main(). Make sure that if you need to reinitialize anything in between runs that you create a method to do so.

win_numb is not initialized (mine happens to say 2 each time). Such a large negative number should raise a red flag that something is not initialized. Note that lines 48 and 49 are useless in your prior listing, and you don't actually assign it anywhere.

This article has been dead for over six months. Start a new discussion instead.