Mos*_*ali 2 c segmentation-fault
我的程序是一个简单的"岩纸剪刀蜥蜴Spock"程序,我想添加一个2人或1个玩家与计算机选项.这是我编写选择部分的方式:
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);
}
Run Code Online (Sandbox Code Playgroud)
当我运行程序时,它在我输入值后立即给出了分段错误playerNum.
上面使用的所有变量都声明为整数.
在传递类型参数时,您scanf期望一个类型的参数.
你在论证中缺少. int *int&scanf
scanf("%d", playerNum);
^
|
& is missing.
Run Code Online (Sandbox Code Playgroud)
将此更改为
scanf("%d", &playerNum);
Run Code Online (Sandbox Code Playgroud)