Thread: guessing game

  1. #1
    Registered User wayko's Avatar
    Join Date
    Sep 2001
    Posts
    28

    Talking guessing game

    #include <iostream.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctime>

    srand();

    int main ()
    {
    int guess;
    guess=1+rand()%100;
    cout << "Guess what number I am thinking of... : ";
    cout << guess;
    cin >> guess;
    while (guess != rand())
    {
    while (guess < rand())
    {
    cout << "The number is higher...Guess again : ";
    cin >> guess;

    }
    while (guess > rand())
    {
    cout << "The number is lower...Guess again : ";
    cin >> guess;

    }
    while (guess == rand())
    {
    cout << "You are correct";
    break;
    }
    }
    return 0;
    }

    im trying to make a guessing game that the program generates a random number between 1 and 100 is there a way to do that?
    hello its me Wayko

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    You want two variables
    Code:
    int my_guess = 1+rand()%100;
    int your_guess = -1;
    while ( my_guess != your_guess ) {
       cout << "What's your guess";
       cin >> your_guess;
       if ( my_guess == your_guess ) {
          // woo hoo!!
       }
    }
    Got the idea?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    25
    You should initially seed the program with this line. Please note that it should only be seeded once or it will return the same number again and again.

    srand( (unsigned)time( NULL ) );



    int main ()
    {
    int guess;
    guess=1+rand()%100;
    cout << "Guess what number I am thinking of... : ";
    cout << guess;
    cin >> guess;
    while (guess != rand())

    The code above should be editted to follow the format of mine below.

    int main()
    {
    int guess;
    const int temp = 1 + (rand()%100);
    cout<<"Guess what number I am thinking of...:\n";
    cin>>guess;
    while(guess != temp)//very important change read below why
    {
    //the rest of your code
    }


    Everytime rand() is called it returns a different random number. So instead of comparing your guess to the initial random number it is compared to a new one each time. Your guessing game could go on for ever this way. The way to avoid this pitfall is to store the number initially created into a const int variable and compare from that.

  4. #4
    Registered User wayko's Avatar
    Join Date
    Sep 2001
    Posts
    28
    it tried it
    it ask that guess be initialized



    #include <iostream.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctime>

    srand();

    int main ()
    {
    int guess = 0;
    const int temp = 1 + (rand()%100);
    cout << "Guess what number I am thinking of... : ";
    cout << guess;
    cin >> guess;
    while (guess != temp)
    {
    while (guess < temp)
    {
    cout << "The number is higher...Guess again : ";
    cin >> guess;

    }
    while (guess > temp)
    {
    cout << "The number is lower...Guess again : ";
    cin >> guess;

    }
    while (guess == temp)
    {
    cout << "You are correct";
    break;
    }
    }
    return 0;
    }

    also the answer is constantly 42

    i tried
    srand( (unsigned)time(null) );
    but i get errors
    hello its me Wayko

  5. #5
    Registered User wayko's Avatar
    Join Date
    Sep 2001
    Posts
    28
    these are the errors

    error C2065: 'null' : undeclared identifier
    error C2501: 'srand' : missing storage-class or type specifiers
    error C2373: 'srand' : redefinition; different type modifiers
    hello its me Wayko

  6. #6
    Registered User
    Join Date
    Sep 2001
    Posts
    25
    Declare srand at the beginning of the main function

    main()
    {
    srand( (unsigned)time( NULL ) );
    //then the rest of your code

    try using #include <time.h>

  7. #7
    Registered User
    Join Date
    Sep 2001
    Posts
    25
    Here it is for you. New and improved and full functional. A few suggestions I might include are:
    1) The program could use a infinite for loop to repeat until a break is called.
    2) Could incorperate a mechanism to catch values higher or lower then the minimum and maximum values allowed.
    3) Could incorperate a scoring system. Something along the lines of getting a higher score for having the fewest guesses.

    This is how I got started into programming. I would make a shell application and slowly add new features. Currently I am working on a RPG game and this is how I evolve it into what I want it to be. I add a feature, test it for any possible errors, fix errors if found, test it again. If it looks good I save the class into a folder I use for backups and then add it into my source code. Doing this gives me a lot of control over what kinda of errors can appear and keeps the number of runningtime errors at a minimal.

    Hope this helps, e-mail me if you have any questions.
    Jeramy

    #include <iostream.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>

    int main ()
    {
    srand( (unsigned)time( NULL ) );
    int guess = 0;
    const int temp = 1 + (rand()%100);
    cout << "Guess what number I am thinking of... : ";
    cin >> guess;
    while (guess != temp)
    {
    while (guess < temp)
    {
    cout << "The number is higher...Guess again : ";
    cin >> guess;
    }
    while (guess > temp)
    {
    cout << "The number is lower...Guess again : ";
    cin >> guess;
    }
    while (guess == temp)
    {
    cout << "You are correct";
    break;
    }
    }
    return 0;
    }

  8. #8
    Registered User
    Join Date
    Aug 2001
    Posts
    72

    out of the topic

    >>
    >> also the answer is constantly 42
    >>

    this is a very good answer :-))))

  9. #9
    Registered User wayko's Avatar
    Join Date
    Sep 2001
    Posts
    28

    yes

    yes 42 is a good answer ñ_ñ

  10. #10
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    aaarggh 42 the meaning of life!
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  11. #11
    Registered User
    Join Date
    Sep 2001
    Posts
    25
    Coincidence I think not.

  12. #12
    Unregistered
    Guest

    Talking

    This should work.

    #include <iostream.h>
    #include <stdlib.h>
    #include <time.h>

    main()
    {
    int low = 0;
    int high = 101;
    int guess = 50;
    int trys = 0;
    int previous = 0;
    char answer = NULL;

    srand(time(NULL));

    cout << "Think of a number between 1-100 and I will guess it!\n" << endl;

    while (answer != 'c' && answer != 'C')
    {
    cout << "My guess is " << guess << "." << endl;
    cout << "Is your number [l]ower, [h]igher or [c]orrect?: ";
    cin >> answer;

    trys++; if (trys == 14) { cout << "\nMoron!\n"; exit(1); }

    if (answer == 'l' || answer == 'L')
    {
    previous = guess;
    while (previous == guess)
    {
    high = guess;
    guess = guess - (rand() % (guess - low));
    }
    }
    else if (answer == 'h' || answer == 'H')
    {
    previous = guess;
    while (previous == guess)
    {
    low = guess;
    guess = guess + (rand() % (high - guess));
    }
    }
    }

    cout << "\nHurray!\n";
    cout << "Even though it took " << trys << " try(s) to get it." << endl;

    return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 20q game problems
    By Nexus-ZERO in forum C Programming
    Replies: 24
    Last Post: 12-17-2008, 05:48 PM
  2. Need book to program game into multiplayer...
    By edomingox in forum Game Programming
    Replies: 3
    Last Post: 10-02-2008, 09:26 AM
  3. 2D RPG Online Game Project. 30% Complete. To be released and marketed.
    By drallstars in forum Projects and Job Recruitment
    Replies: 2
    Last Post: 10-28-2006, 12:48 AM
  4. Random guessing game
    By Nalif in forum C Programming
    Replies: 16
    Last Post: 10-26-2006, 03:05 AM
  5. Game Independent Anti-cheat Project Needs Programmers
    By GIA Project Lea in forum Projects and Job Recruitment
    Replies: 3
    Last Post: 09-15-2005, 07:41 PM