Like Tree1Likes
  • 1 Post By AndiPersti

Guessing Game

This is a discussion on Guessing Game within the C Programming forums, part of the General Programming Boards category; This is what I have for a user-defined range and everything works properly until I upload it to the upload ...

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    1

    Guessing Game

    This is what I have for a user-defined range and everything works properly until I upload it to the upload site. The first run works but the second run gives an infinite output of "Too Low". What am I doing wrong?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define min(a,b) ((a)>(b)?(b):(a))
    int main ()
    {
        unsigned int a,b,count=0,range;
        unsigned int guess=0,answer;
        printf("Enter 2 integers for the range\n");
        scanf("%d %d", &a, &b);
        printf("I'm thinking of a number between %d and %d\n", a, b);
        range=b-a;
        srand(min(a,b));
        answer=(rand()%(range+1)+a);
        while(guess!=answer)
        {
        printf("Pick a number\n"); 
        scanf("%d",&guess);
        if(guess==answer)
            {
            printf("You guessed correctly\n");
            }
        else if(answer< guess)
            {
                printf("Your guess is too high\n");
            }
        else
            {
                printf("Your guess is too low\n");
            }
        count++;
        }
        printf("It took you %d time(s)\n", count);
        return 0;
    }

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    If you want to have the user able to play more than one time, you need to add a second while loop, around everything you have here, EXCEPT the srand() line of code. That line should be moved up and only executed ONE time (not once per game).

    Other than that, line #8 is where the new while loop should start, and it should extend to where the user shows he doesn't want to play any more guessing games (ready to exit).

    If that's not your question, then show a game or two, and what it is that you want here.

  3. #3
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by ryansanford View Post
    This is what I have for a user-defined range and everything works properly until I upload it to the upload site.
    What do you mean with "upload it to the upload site?

    Quote Originally Posted by ryansanford View Post
    The first run works but the second run gives an infinite output of "Too Low". What am I doing wrong?
    It is always easier for us if you tell us for which inputs the program doesn't work.
    So my guess it that the second time you have inputted the higher limit before the lower one, right?
    Code:
    $ ./test
    Enter 2 integers for the range
    3 1
    I'm thinking of a number between 3 and 1
    Pick a number
    2
    Your guess is too low
    Pick a number
    3
    Your guess is too low
    Pick a number
    Now look at your code:
    Code:
    unsigned int a,b,count=0,range;
    unsigned int guess=0,answer;
    printf("Enter 2 integers for the range\n");
    scanf("%d %d", &a, &b);
    printf("I'm thinking of a number between %d and %d\n", a, b);
    range=b-a;
    srand(min(a,b));
    answer=(rand()%(range+1)+a);
    You assume that "a" will always be the smaller number. But if "b" is the smaller one, "range" will be negative. Because "range" is unsigned, it's value is (b - a) + (UINT_MAX + 1) which is probably a very big number (e.g. in my example (1 - 3) = -2, adding UINT_MAX + 1 on my system (32bit integers) will be -2 + 4294967295 + 1 = 4294967294).
    As a consequence "answer" will probably be a very big number too.

    Another problem is how you use srand(). Since you seed the pseudorandom number generator with the smaller value of "a" and "b", you will always get the same "random" number if you play the game several times with the same input values.

    Bye, Andreas
    Last edited by AndiPersti; 10-01-2012 at 03:08 AM. Reason: typo
    poornaMoksha likes this.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Guessing game: how to quit the game?
    By hzr in forum C Programming
    Replies: 5
    Last Post: 12-18-2008, 09:53 AM
  2. guessing game
    By Sal79 in forum C Programming
    Replies: 14
    Last Post: 05-09-2007, 02:22 PM
  3. guessing game
    By Inferno in forum C++ Programming
    Replies: 2
    Last Post: 09-22-2004, 05:37 PM
  4. A guessing game
    By Lyanette in forum C++ Programming
    Replies: 5
    Last Post: 04-03-2003, 09:02 AM
  5. guessing game
    By wayko in forum C++ Programming
    Replies: 11
    Last Post: 09-19-2001, 06:10 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21