Magic Number game

Posts 1–12 of 12 · Page 1 of 1
Magic Number game
So in my book we were learning about loops, and a perfect example to use a loop was the guess a number game. So I took the code and figured out how to use loops and all that, and even added a thing or 2 to the game. What I want to know is how would I make it so after you win and guess the number, you have the option to play again? I experimented a bit but 5 days of c++ studying, I haven't got the knowledge to do this.

Code:
#include <iostream>
#include <cstdlib>
using namespace std;

int main()
{
	int guess;
	int number;
	int response;

	number = rand();


	cout << "Would you like to play the magic number game?\n";
	cout << " 1 for yes or 2 for no\n";
	cin >> response;

	if(response == 1) {
		do {
			cout << "Enter your guess: ";
			cin >> guess;

			if(guess == number) cout << "You're right\nBye\n";
			if(guess > number) cout << "You're too high\n";
			if(guess < number) cout << "You're too low\n";
		}
		while(guess != number);
	}
	else cout << "Bye\n";

	return 0;
}
You need to seed your rand, ( srand() ) If you don't you'll see that you'll get a certain pattern in your guesses, and we don't want that do we?
Quote Originally Posted by schim View Post
You need to seed your rand, ( srand() ) If you don't you'll see that you'll get a certain pattern in your guesses, and we don't want that do we?
Maybe that could be why it's always 41 XD. Anywys could you explain what seeding is/does. Still learning and all.
Quote Originally Posted by ez2animate View Post
Maybe that could be why it's always 41 XD. Anywys could you explain what seeding is/does. Still learning and all.
to create a seed, do this

Code:
#include <time.h>

srand((unsigned)time(0))
what this does is it takes the current time, and uses that as the number the rand() function will apply its algorithm to. As a result, as time passes, the number that rand() produces will differ.

Hope i helped.
seed the random to the milliseconds for somewhat 100% random numbers
Quote Originally Posted by Auxilium View Post
seed the random to the milliseconds for somewhat 100% random numbers
How could would I do that Koreans/Artemis? I have just been learning for 5 days so I'm not very educated in the ways of C++.
What I want to know is how would I make it so after you win and guess the number, you have the option to play again?
You could use the do while loop.

Code:
do {
   //.... your code
   char answer;
   cout << "Want to play again [y/n]? ";
   cin >> answer;
} while(answer == 'y');
Quote Originally Posted by Gordon` View Post
You could use the do while loop.

Code:
do {
   //.... your code
   char answer;
   cout << "Want to play again [y/n]? ";
   cin >> answer;
} while(answer == 'y');
Alright I'll try that. See the book used the do while loop also, but they made it so that the game exits when you win. I guess learning a language and programming is thinking outside the box.
[php]
srand((unsigned)time(0)); //seed to time
int number = rand() % 100; //set number as random smaller than 100

[/php]

try that

not sure how it will work because i learned randoms just for this
Quote Originally Posted by Auxilium View Post
[php]
srand((unsigned)time(0)); //seed to time
int number = rand() % 100; //set number as random smaller than 100

[/php]

try that

not sure how it will work because i learned randoms just for this
Nah that still isn't enouch use %1000 or something
Quote Originally Posted by schim View Post
Nah that still isn't enouch use %1000 or something
well just add a 0 then /
Code:
#include <time.h>
class EpicRandoms
{
     Initialize()
     {
         srand(time(0));
     }
     int Random(int minimum=0, int maximum=1000)
     {
         return (rand()%(maximum-minimum)+minimum);
     }
}
Might not compile(too lazy to check)
Posts 1–12 of 12 · Page 1 of 1

Post a Reply

Similar Threads

Tags for this Thread

None

Talk with us