HiveBrain v1.2.0
Get Started
← Back to all entries
patterncMinor

"Guess the Number" game in C

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
numbergametheguess

Problem

This is my first program in C. I'd rather not form any bad habits now. Is there anything that looks like bad practice, or something just looks wrong?

#include 
#include 
#include 

int main(void) {
    srand(time(NULL));
    int r = rand() % 10 + 1;
    int correct = 0; 
    int guess; 
    int counter = 0; 

    printf("Guess my number! "); 

    do {
        scanf("%d", &guess);
        if (guess == r) {
            counter++;
            printf("You guessed correctly in %d tries! Congratulations!\n", counter);
            correct = 1; 
        }

        if (guess  r) { 
            counter++; 
            printf("Your guess is too high. Guess again. ");
        }
    } while (correct == 0);

    return 0;
}

Solution

Here are some of the improvements i can suggest:

  • move your counter variable , from the inside the if statements , as increasing the count is independent of the if condition.



-
Use srand(time(NULL)); , below the declarations , or else , your compiler may throw this warning , when compiled using C90 standard.

warning: ISO C90 forbids mixed declarations and code [-pedantic]

So to ensure portability , avoid statements between declarations.

-
In the long run , more meaningful and formal names , can be used instead of names like r and guess.

In my personal opinion , these two changes would be better:

-
Using a break; , when the correct number is guessed , can eliminate the need for using an extra variable correct.

-
I would prefer to use just a while() instead of do{}while();, if the program logic permits it.

Here is the program , modified with the changes , i listed above:

#include 
#include 
#include 

int main(void) 
{

int random_num = 0;
int guessed_num = 0;
int counter = 0; 

srand(time(NULL));
random_num = rand() % 10 + 1;

printf("Guess my number! "); 

    while(1)
    {
        counter++; 

        scanf("%d", &guessed_num);

        if (guessed_num == random_num) 
        {
            printf("You guessed correctly in %d tries! Congratulations!\n", counter); 
            break;
        }

        if (guessed_num  random_num) 
            printf("Your guess is too high. Guess again. ");

    } 

return 0;   
}

Code Snippets

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

int main(void) 
{

int random_num = 0;
int guessed_num = 0;
int counter = 0; 

srand(time(NULL));
random_num = rand() % 10 + 1;

printf("Guess my number! "); 

    while(1)
    {
        counter++; 

        scanf("%d", &guessed_num);

        if (guessed_num == random_num) 
        {
            printf("You guessed correctly in %d tries! Congratulations!\n", counter); 
            break;
        }

        if (guessed_num < random_num) 
            printf("Your guess is too low. Guess again. ");

        if (guessed_num > random_num) 
            printf("Your guess is too high. Guess again. ");

    } 

return 0;   
}

Context

StackExchange Code Review Q#24366, answer score: 7

Revisions (0)

No revisions yet.