#Number guessing game

1 messages · Page 1 of 1 (latest)

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

/******************************
*Takes no arguments.
*Returns user guess as int.
******************************/
int takeUserGuess() {
    int guess;
    printf("input your guess: ");
    if(scanf("%d",&guess) == 1) {
        printf("correct input \n");
    } else {
        printf("incorrect input, expect an integer\n");    
    }
    while(getchar() != '\n');
    return guess;    
}

/****************************
*Takes two arguments,
*guess and solution.
*
*Compares guess and solution,
*returns an int.
*
*0 if guess < solution,
*1 if guess == solution,
*2 if guess > solution,
*****************************/
int  checkUserGuess(int guess, int solution) {
    if(guess < solution) return 0;
    if(guess == solution) return 1;
    else return 2;
}

int main() {
    srand(time(NULL));
    int solution = rand() % 101;
    int attempts = 0;
    /************
    * set to 1 if user guessed
    * leave at zero otherwise
    ***********/
    int isCorrect = 0;    
    while(attempts < 5) {
        int guess = takeUserGuess();
        int result = checkUserGuess(guess, solution);
        attempts++;        
        if(result == 0) {
            printf("too low!\n");
        } else if(result == 1) {
            printf("you guessed correctly!\n");
            isCorrect = 1;
            break;
        } else {
            printf("too high!\n");
        }
    }
    
    if(isCorrect == 0) {
        printf("unfortunately, you didn't guess correctly ;c, better luck next time!");
    } else {
        printf("wow! you guessed correctly! YIPPEEE!");
    }

    
    return 0;
}

simple game where you guess a random number

real salmon
#
  • in takeUserGuess(), if the user provides an incorrect input (ie, not an integer), the program proceeds with an uninitialized value for guess. This is a bug, and you should handle input validation, for example by using a loop to keep asking the user until a valid input is provided.
  • you should also use constants for magic numbers, like:
const int MAX_RANGE = 101;
const int MAX_ATTEMPTS = 5;
  • isCorrect is essentially capturing the same state that could be inferred from result - this is redundant and you should simplify by replacing isCorrect with direct checks on result

other than that lgtm

alpine trellis
#

thank you so much! but i don't think i understand your last point, could you maybe explain it a little more?

real salmon
#

maybe it would be easier to just see an example:

int main() {
    srand(time(NULL));
    int solution = rand() % MAX_RANGE;
    int attempts = 0;

    while(attempts < MAX_ATTEMPTS) {
        int guess = takeUserGuess();
        int result = compareGuessAndSolution(guess, solution);
        attempts++;

        if(result == 0) {
            printf("Too low!\n");
        } else if(result == 1) {
            printf("You guessed correctly!\n");
            return 0;
        } else {
            printf("Too high!\n");
        }
    }
    printf("Unfortunately, you didn't guess correctly. Better luck next time!");
    return 0;
}
alpine trellis
#

ooooh

#

that's neat

#

thank you

#

i didn't know you can just return 0 like that

#

that's neat

real salmon
#

yep! hap

alpine trellis
#
int takeUserGuess() {
    while(1){
        int guess;
        printf("input your guess: ");
        if(scanf("%d",&guess) == 1) {
            printf("correct input \n");
            return guess;
        } else {
            printf("incorrect input, expect an integer\n");    
        }
        while(getchar() != '\n');
    }    
}
real salmon
#

good job!

noble wing
#

FWIW don't use rand(), use the facilities in <random>.

alpine trellis
#

Is there much of a difference?

toxic junco
alpine trellis
#

yeah it's C