0

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.

Danny
  • 508
  • 1
  • 3
  • 22
Mrunmai Dahare
  • 118
  • 1
  • 6

2 Answers2

1

Your scanf is incorrect:

scanf("%d", guess); // should be scanf("%d", &guess);
artm
  • 16,750
  • 4
  • 31
  • 51
0

There is no problem with your if-statement. The problem is that scanf() takes a format string and a pointer, but not a format and a non-pointer variable.

Danny
  • 508
  • 1
  • 3
  • 22