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?
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; }
[IMG]https://i298.photobucke*****m/albums/mm280/Ez2animate/mynewsig.png[/IMG]
TO DO LIST
Attempt to learn C++ [✔]Understand the basics of C++ [✔]Reach halfway through my book [✔]Make my OWN original simple program []Finish my book []Make my own hack[]
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?
[IMG]https://i298.photobucke*****m/albums/mm280/Ez2animate/mynewsig.png[/IMG]
TO DO LIST
Attempt to learn C++ [✔]Understand the basics of C++ [✔]Reach halfway through my book [✔]Make my OWN original simple program []Finish my book []Make my own hack[]
to create a seed, do this
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.Code:#include <time.h> srand((unsigned)time(0))
Hope i helped.
seed the random to the milliseconds for somewhat 100% random numbers
[IMG]https://i298.photobucke*****m/albums/mm280/Ez2animate/mynewsig.png[/IMG]
TO DO LIST
Attempt to learn C++ [✔]Understand the basics of C++ [✔]Reach halfway through my book [✔]Make my OWN original simple program []Finish my book []Make my own hack[]
You could use the do while loop.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?
Code:do { //.... your code char answer; cout << "Want to play again [y/n]? "; cin >> answer; } while(answer == 'y');
[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
Last edited by Auxilium; 08-16-2010 at 07:41 PM.
Might not compile(too lazy to check)Code:#include <time.h> class EpicRandoms { Initialize() { srand(time(0)); } int Random(int minimum=0, int maximum=1000) { return (rand()%(maximum-minimum)+minimum); } }
Ah we-a blaze the fyah, make it bun dem!
[IMG]https://i298.photobucke*****m/albums/mm280/Ez2animate/mynewsig.png[/IMG]
TO DO LIST
Attempt to learn C++ [✔]Understand the basics of C++ [✔]Reach halfway through my book [✔]Make my OWN original simple program []Finish my book []Make my own hack[]