dimanche 2 août 2020

My C program is not giving desired output

I am learner and new to C. I am making a number guess game using C but it is not executing properly, my if statement is not getting executed in this code please help out.

 #include<stdio.h>
    #include<stdlib.h>
    #include<time.h>
    int main(){
        int num, guess, count;
        count = 1;
        srand(time(0));
        num = (rand()%10 + 1);

        do
        {
            printf("Guess the number: \n");
            scanf("%d", guess);
            if(guess>num)
            {
                printf("too large");
            }
            else if(guess<num)
            {
                printf("too small");
            }
            else
            {
                printf("you won!\nIn %d count.", count);
            }
            
            count++;
            
            
        }while(guess != num);
        
        return 0;
    }

Code was supposed to give output as

 Guess the number
     5
    too small!
    Guess the number
    7
    You won in 2 count

But it is not executing if else statement and breaking the loop after scanf function. Please help.

Aucun commentaire:

Enregistrer un commentaire