0

I'm trying to code a program there I use a function to generate a number between 1-100 and compare it with user input but something it's wrong? I'm trying to make the program a random new number when the user guesses the right number.

int main(void) {

int playAgain = 1, guessNumber, r, i = 0;

printf ("I'm thinking of a number between 1 and 100, guess which!\n\n");

while (playAgain == 1){

    while (1) {

        printf("Guess: ");
        scanf_s("%d", &guessNumber);
        i++;

        if (guessNumber == generateNumber(r)) {
            printf("Congratulations, that is correct!\n\n");
            break;
        }
        else if (guessNumber > generateNumber(r)) {
            printf("Your guess is too high, try again!\n");
        }
        else {
            printf("Your guess is too low, try again!\n");
        }
    }

    printf("You made %d attempts.", i);
    printf("\nShall we play again (1 for yes, 0 for no)? ");
    scanf_s("%d", &playAgain);
}

return 0;
}

int generateNumber(int randomNumber) {
   srand(time(NULL));
   randomNumber = rand() % 100 + 1;
   return randomNumber;
}
LeoParden
  • 17
  • 2
  • 1
    You should state your problem as well. – Eugene Sh. Nov 24 '20 at 21:53
  • https://stackoverflow.com/questions/7343833/srand-why-call-it-only-once – user3386109 Nov 24 '20 at 21:56
  • *something is wrong*. Please tell us more specifically what error or incorrect behaviour you observe. – kaylum Nov 24 '20 at 21:56
  • 1
    You should only call `generateNumber(r)` once and store that result for later comparison. – 001 Nov 24 '20 at 21:57
  • 5
    In addition to the `srand` problem you are calling `generateNumber` repeatedly. Which means that every single comparison may be comparing against a different number. Unlikely what you want. Probably you want to call `generateNumber` once outside the loop, assign the result to a variable and use that variable inside the loop. – kaylum Nov 24 '20 at 21:58
  • Edit the question to provide a [mre], including input that reproduces the problem, observed output, the output desired instead, and an explanation of what is wrong in the observed behavior. – Eric Postpischil Mar 28 '22 at 18:22

1 Answers1

-2
int Number, Guess; 
void guessnum(Guess, Number){
    int NumofGuesses = 1;
    do
    {
        if (Guess > Number){ 
            printf("lower");
        }
        else if (Guess < Number){            
            printf("higher");
        }
        else{
            printf("You guessed it in %d tries.\n", NumofGuesses);
            break;
        }
        scanf_s("%d", &Guess);
        NofGuesses++;
    } while (1);
    return;
}    
int main(){
    srand(time(0));
    Number = rand() % 100 + 1;        
    while (1) {
        printf("Guess a number (1 - 100): ");
        scanf_s("%d", &Guess);
        guessnum(Guess, Number);
        printf("Do you want to go again? y or n: ");
        char ch = _getwch();
        if (ch == 'n' || ch == 'N')
            break;
    }
    return 0;
}
  • 1
    Code without explanation is not a good answer. – Eric Postpischil Mar 28 '22 at 18:22
  • nik, `_getwch()` is not in the standard C library. MS [_getwch()](https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/getch-getwch?view=msvc-170) returns a `wint_t`, not `char`. Certainly `char ch = _getwch();` will first read the left-over `'\n'` from `scanf_s("%d", &Guess);`. – chux - Reinstate Monica Mar 28 '22 at 18:22