user3022392
user3022392

Reputation: 3

'If' inside the 'For' crashes the game - C language

Here is a code: (whyerror1.exe)

#include <stdio.h>
#include <stdlib.h>
int i, n, health=100;
int main(void)
{
    for (i=0; i<1; i++)
    {
        printf("health: %d\n",health);
        printf("There is meat on the ground.\n");
        printf("Will you eat it?\n");
        printf("1: yes  2: no\n"); 
        scanf("%d", n);
        if(n==1) {
                      system("cls");
                      printf("Ate it. \n");
                      printf("There is no people, so it is safe to eat. \n");
                      printf("Health is increased.\n");
                      system("pause>nul");
                      system("cls");
                      }
        else if(n==2) {
                      system("cls");
                      printf("Didn't ate it. \n");
                      printf("More hunger, less health. \n");
                      system("pause>nul");
                      system("cls");
                      }
        else {
                     printf("select between 1 and 2. \n");
                     system("pause>nul");
                     system("cls");
                     i=-1;
        }
    }
}

Result: whyerror1.exe stopped working. Why is this happening? I already tried selecting other numbers. I tried any numbers! Plz help!

Upvotes: 0

Views: 69

Answers (1)

xorguy
xorguy

Reputation: 2744

scanf("%d", n);

Should be:

scanf("%d", &n);

Upvotes: 4

Related Questions