-2

I have to make this Guess the Number program. It asks the user to insert a range (min-max), then the user has to think of a number and the program tries to guess it. At every attempt, the user says if the number is bigger or smaller, there're no limited attempts. I made the program's guess number generated randomly, but the problem is that I don't know how to make sure the program doesn't ask again a number that's already been discarded before. To explain what shouldn't happen:

  • I insert the range 30-40
  • I think of 37
  • The program says: is your number 40? n
  • Is bigger or smaller? <
  • The program says: is your number 34? n
  • Is bigger or smaller? >
  • The program says: is your number 40? (40 has already been asked)

I've just started studying C.

That's the code:

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

int main()
{
    int numGen;
    int minimum = 1;
    int maximum = 0;
    char answer;
    char answer2;

    while(minimum>maximum || minimum==0){

        printf("Insert range \'min-max\'\n");
        scanf("%d-%d", &minimum, &maximum);
        if(minimum<=maximum && minimum>0) break;
        else printf("Invalid range\n\n");
    }

    while(1) {

        numGen = minimum + (rand()+time(NULL))%(maximum+1-minimum);

        printf("Is your number: %i \'y\\n\'\n", numGen);
        scanf(" %c", &answer);
        if(answer=='y') {
            printf("\nI guessed!\n");
            break;
        }
        else if(answer=='n') {
        printf("Is it bigger or smaller? \'>\\<\'\n");
        scanf(" %c", &answer2);
        if(answer2=='>') minimum = numGen;
        else if(answer2=='<') maximum = numGen;
        }
    }

    return 0;
}

TylerH
  • 20,799
  • 66
  • 75
  • 101
  • How many times will the game be played in one session? 1000? Generate an array of 1000 sequential numbers (none the same), or maybe with gaps (by adding a small random number instead of 1) and shuffle (swap) them randomly. Then use them one by one from the array. – Weather Vane Oct 09 '20 at 15:48
  • @WeatherVane We've not studied arrays yet, so I was searching for a simpler way to solve it. Thanks anyway, I'll try it. –  Oct 09 '20 at 15:57
  • The first `while` loop should be `while (1)`. The condition is checked in the body of the loop, and the `break` statement ends the loop. So checking again in the `while` statement is redundant. – user3386109 Oct 09 '20 at 15:57
  • @user3386109 Thanks I didn't notice. –  Oct 09 '20 at 16:01

1 Answers1

1

You are generating a random number between minimum and maximum, both inclusive.

When numGen is not the number to guess, it should be eliminated from the range.

This mean that

        if(answer2=='>') minimum = numGen;
        else if(answer2=='<') maximum = numGen;

should be

        if(answer2=='>') minimum = numGen + 1;
        else if(answer2=='<') maximum = numGen - 1;

Also, instead of adding time(NULL) to the random number, you may want to use srand() to get different random numbers.

For more information, see c - srand() — why call it only once? - Stack Overflow.

TylerH
  • 20,799
  • 66
  • 75
  • 101
MikeCAT
  • 73,922
  • 11
  • 45
  • 70