C代码崩溃(不明白为什么)

时间:2015-11-01 18:44:46

标签: c

基本上我是初学者编码器,这就是我写的:

#include <stdio.h>
#include <stdlib.h>

int main()
{
system("COLOR 0A");

char playerName[13];
char playerGender;
int playerAge;

printf("Please input your name and age!\nName: ");
scanf("%s", playerName);
printf("Age (from 18 to 50): ");
scanf("%d", &playerAge);

label:
if(playerAge > 18 && playerAge < 50)
{

    printf("What gender are you, M(male) or F(female): ");
    scanf("%c", playerGender);

    gender:
    if(playerGender == 'M' || playerGender == 'F'){

        printf("Okay, so your name is %s, you're %d years old and you're a %s.", playerName, playerAge, playerGender);
    }else{
        printf("Try again.\n\n"
           "What gender are you, M(male) or F(female): ");
           scanf("%c", playerGender);
           goto gender;
    }
}else{

        printf("Wrong, try again.\n"
               "Age (from 18 to 50): ");
        scanf("%d", &playerAge);
        goto label;
}


return 0;
}

当我将所需年龄继续下去时,scanf的{​​{1}}会崩溃。在它向我显示关于我的性别的问题之后?我的错误在哪里?

2 个答案:

答案 0 :(得分:10)

尝试:

scanf("%c", &playerGender);

而不是

scanf("%c", playerGender);

因为scanf接受指针而不是对您要填充的变量的引用。

答案 1 :(得分:0)

我也是初学者,但我认为你需要这样写scanf("%s", playerName); scanf("%12s", playerName);

BTW让我知道这是否有效。