Results 1 to 12 of 12
  1. #1
    ez2animate's Avatar
    Join Date
    Aug 2009
    Gender
    male
    Posts
    204
    Reputation
    10
    Thanks
    20

    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;
    }
    [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[]

  2. #2
    schim's Avatar
    Join Date
    May 2010
    Gender
    male
    Location
    My chair
    Posts
    367
    Reputation
    10
    Thanks
    114
    My Mood
    Twisted
    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?

  3. #3
    ez2animate's Avatar
    Join Date
    Aug 2009
    Gender
    male
    Posts
    204
    Reputation
    10
    Thanks
    20
    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.
    [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[]

  4. #4
    scimmyboy's Avatar
    Join Date
    Jan 2008
    Gender
    male
    Location
    https://mpgh.net MPGHCash: $442,596,199
    Posts
    5,645
    Reputation
    26
    Thanks
    896
    My Mood
    Happy
    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.

  5. #5
    Auxilium's Avatar
    Join Date
    Jul 2010
    Gender
    male
    Location
    深い碧の果てに
    Posts
    4,518
    Reputation
    445
    Thanks
    609
    My Mood
    Happy
    seed the random to the milliseconds for somewhat 100% random numbers

  6. #6
    ez2animate's Avatar
    Join Date
    Aug 2009
    Gender
    male
    Posts
    204
    Reputation
    10
    Thanks
    20
    Quote Originally Posted by Koreans 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++.
    [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[]

  7. #7
    Gordon`'s Avatar
    Join Date
    Dec 2007
    Gender
    male
    Posts
    283
    Reputation
    24
    Thanks
    325
    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');


  8. #8
    Auxilium's Avatar
    Join Date
    Jul 2010
    Gender
    male
    Location
    深い碧の果てに
    Posts
    4,518
    Reputation
    445
    Thanks
    609
    My Mood
    Happy
    [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.

  9. #9
    schim's Avatar
    Join Date
    May 2010
    Gender
    male
    Location
    My chair
    Posts
    367
    Reputation
    10
    Thanks
    114
    My Mood
    Twisted
    Quote Originally Posted by Koreans 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

  10. #10
    Hell_Demon's Avatar
    Join Date
    Mar 2008
    Gender
    male
    Location
    I love causing havoc
    Posts
    3,976
    Reputation
    343
    Thanks
    4,320
    My Mood
    Cheeky
    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)
    Ah we-a blaze the fyah, make it bun dem!

  11. #11
    Auxilium's Avatar
    Join Date
    Jul 2010
    Gender
    male
    Location
    深い碧の果てに
    Posts
    4,518
    Reputation
    445
    Thanks
    609
    My Mood
    Happy
    Quote Originally Posted by schim View Post
    Nah that still isn't enouch use %1000 or something
    well just add a 0 then /

  12. #12
    ez2animate's Avatar
    Join Date
    Aug 2009
    Gender
    male
    Posts
    204
    Reputation
    10
    Thanks
    20
    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.
    [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[]

Similar Threads

  1. [Release] Guess Number Game
    By Aggorance in forum C++/C Programming
    Replies: 14
    Last Post: 09-02-2011, 06:18 PM
  2. Combat Arms New Number 1 In Game
    By deathatkill in forum Spammers Corner
    Replies: 1
    Last Post: 06-08-2011, 04:59 PM
  3. [Release] Guess That Number Game
    By sherkun in forum Visual Basic Programming
    Replies: 11
    Last Post: 11-17-2010, 02:32 AM
  4. The Number Game
    By Austin in forum Spammers Corner
    Replies: 7
    Last Post: 06-17-2010, 03:38 AM
  5. THE NUMBER GAME!!!!
    By zebramanz in forum General
    Replies: 27
    Last Post: 03-29-2009, 11:09 PM