Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

neobie

Guessing Game by OOD

This topic is 5227 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

i'm trying to program simple stuff using object oriented design.. is this how it's suppose to be done??
#include <iostream>
#include <windows.h>
using namespace std;

class magicnum
{
public:
	int GenNum()
	{
		srand(GetTickCount()); 
		num1 = rand()%11;

		return num1;
	
	} // generates the random magic number;

	int GetNum() { return num1; } // returns the magic number;


private:
	int num1; // the "answer";

};

int main()
{
	magicnum magicNumber;
	magicNumber.GenNum();

	int choice;
	
	do{
		cout << "Pick a number between 0 and 10, inclusive: ";
		cin >> choice;
		if ( choice == magicNumber.GetNum() )
		{
			cout << "You win!\n";
		}
		else if ( choice < magicNumber.GetNum() )
		{
			cout << "You lost! Guess too low!\n";
		}
		else if ( choice > magicNumber.GetNum() )
		{
			cout << "You lost! Guess too high!\n";
		}
	}while( choice != magicNumber.GetNum() );

	return 0;
}
[edited by - neobie on June 1, 2004 7:37:47 PM]

Share this post


Link to post
Share on other sites
Advertisement
1. Usually you only want to call srand() once. Call it in magicnum''s constructor.

2. If you compare choice to magicNumber.GetNum() you will always be comparing choice againt a different number. Make a variable to store the value of magicNumber.GetNum() at the beginning of the program and compare choice to that variable.


Thanks Salsa!Colin Jeanne | Invader''s Realm
"I forgot I had the Scroll Lock key until a few weeks ago when some asshole program used it. It even used it right" - Conner McCloud

Share this post


Link to post
Share on other sites
You just mixed his gen Num() and get Num() methods there. However the first point is good.. replace genNum by the constructor. Much cleaner, unless you want to generate multiple numbers.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!