Mostafa Al Kamali
Mostafa Al Kamali

Reputation: 19

segmentation fault for C

My program is a simple "rock paper scissor lizard Spock" program and I wanted to add a 2 player or 1 player vs computer option. This is how I coded the choice part:

printf("1.One player OR 2.Two players?\n");
scanf("%d", playerNum);
//input section
printf("1.rock\n2.paper\n3.scissor\n4.spock\n5.lizard\n");
printf("player A enter a choice ");
scanf ("%d", &choiceA);
if (playerNum == 2)
{
    printf("1.rock\n2.paper\n3.scissor\n4.spock\n5.lizard\n");
    printf("player B enter a choice ");
    scanf ("%d", &choiceB);
}
else if (playerNum == 1)
{
    choiceB = 1+rand()%5;
    printf("The computer has picked %d", choiceB);
}

When I run the program it gives me a segmentation fault right after I enter a value for playerNum.
All the variables used in the above are declared as integers.

Upvotes: 2

Views: 94

Answers (1)

haccks
haccks

Reputation: 106112

Your scanf expects an argument of int * type while you are passing it int type argument.
You are missing & in scanf's argument.

scanf("%d", playerNum);
           ^
           |
        & is missing.  

Change this to

scanf("%d", &playerNum);

Upvotes: 7

Related Questions