user28374
user28374

Reputation: 89

Incorrect output on program for a guessing game

I have run two test sets of ranges for a guessing game, 1) 1 to 100 and 2) 50 to 90.

The first set gives the correct output, but my second test range outputs "Too High" only as a result, where as it should run 8 values through that are too low, a ninth that is too high, and then guess correctly on the 10th try.

Here's my code, and any help at all is really appreciated, thanks!

Version 1

Original code

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

int main()
{
    int min, max, number, guess, count = 0;

    printf("Please enter two integers for the range:\n");
    scanf("%d %d", &min, &max);

    number = (min + rand()%(max - min + 1));
    printf("I'm thinking of a number between %d and %d.\n", min, max);

    printf("Enter your guess:\n");
    scanf("%d", &guess);

    while(guess != number)
    {

        if(guess < number)
        {
            count++;
            printf("Too low.\n");
            printf("Enter your guess:\n");
            scanf("%d", &guess);
        }
        else
        {
            count++;
            printf("Too high.\n");
            printf("Enter your guess:\n");
            scanf("%d", &guess);
        }
    }

    count++;
    printf("Correct, it took you %d tries to guess my number. ", count);

    return 0;
}

Version 2

Modified code based on feedback from answers

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

int main()
{
    int min, max, number, guess, count = 0;

    printf("Please enter two integers for the range:\n");
    scanf("%d %d", &min, &max);

    srand(time(NULL));
    number = (min + rand()%(max - min + 1));
    printf("I'm thinking of a number between %d and %d.\n", min, max);
    scanf("%d", &guess);

    do
    {
        printf("Enter your guess:\n");
        scanf("%d", &guess);

        if ( guess < number)
        {
            printf("Too low.\n");
        }
        else if ( guess > number)
        {
            printf("Too high.\n");
        }
    }
    while (guess != number && count++);

    count++;
    printf("Correct, it took you %d tries to guess my number. ", count);

    return 0;
}

Upvotes: 2

Views: 644

Answers (3)

Jonathan Leffler
Jonathan Leffler

Reputation: 753990

Here's some code that works for me. At the moment, for debugging purposes, it prints out the number in the 'Guess a number between x and y' line (which can be helpful in the early stages). It also tests for a number of error conditions which yours does not. The 'out of range' messages are optional; the 'too low' and 'too high' messages do convey the correct information. Notice that the code echoes the information entered — trying to debug the wrong input (you thought you entered A but the program thinks you entered B) can lead to endless debugging confusion.

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

static int get_guess(int *guess)
{
    printf("Enter your guess: ");
    if (scanf("%d", guess) != 1)
        return EOF;
    return 0;
}

int main(void)
{
    int min, max, number, guess, count = 0;
    srand(time(0));

    printf("Please the minimum and maximum values for the range: ");
    if (scanf("%d %d", &min, &max) != 2)
    {
        printf("Oops!\n");
        return 1;
    }
    if (min <= max)
    {
        printf("Your minimum (%d) is not smaller than your maximum (%d)\n", min, max);
        return 1;
    }

    number = (min + rand()%(max - min + 1));
    printf("I'm thinking of a number between %d and %d. (%d)\n", min, max, number);

    while (count < (max - min + 1) && get_guess(&guess) != EOF)
    {
        count++;
        if (guess < min)
            printf("%d: Your guess is smaller than the minimum (%d)\n", guess, min);
        else if (guess > max)
            printf("%d: Your guess is larger than the maximum (%d)\n", guess, max);
        else if (guess < number)
            printf("%d: Too low.\n", guess);
        else if (guess > number)
            printf("%d: Too high.\n", guess);
        else
        {
            printf("%d: Correct, it took you %d tries to guess my number.\n", guess, count);
            break;
        }
    }
    if (guess != number && count >= (max - min))
        printf("You didn't guess the number (%d) after %d tries.\n", number, count);

    return 0;
}

Upvotes: 1

lsiebert
lsiebert

Reputation: 667

Your issue is that your else condition always triggers when the number is equal, asking for a new number.

basically the opposite of:

if (guess < number)

is:

if (guess >= number)

you need to check for >, not >=

You can also reduce code by using a do while loop.

do {
    count++;  
    printf("Enter your guess:\n");
        scanf("%d", &guess);
      if ( guess < number) 
        printf("Too low.\n");
     else if ( guess > number)
          printf("Too high.\n");
   }while (guess != number);

edit: shouldn't have put count increment in the while test.

Upvotes: 4

Jay Nebhwani
Jay Nebhwani

Reputation: 976

rand() is always returning the same number (or sequence of numbers if you were to call it more than once in your program execution) because it always uses the same seed number to generate random numbers. This results in a guess numbers always being the same for a given test case.

To have a different seed each time you execute the program, add the line srand(time(NULL)); before your call to rand().

Upvotes: 1

Related Questions