C 代码崩溃(不知道为什么)

标签 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;
}

当我输入所需的年龄继续时,它在 playerGenderscanf 上崩溃。在它向我显示有关我的性别的问题之后?我的错误在哪里?

最佳答案

尝试:

scanf("%c", &playerGender);

代替

scanf("%c", playerGender);

因为 scanf 采用指针而不是对您要尝试填充的变量的引用。

关于C 代码崩溃(不知道为什么),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33465821/

相关文章:

java - 使用 Java 中的标准输入/输出与 C 程序通信

c - 在c中递归地在末尾插入单链表

c - "%"符号后面的数字有什么作用?

通过试验划分进行素数条件测试

c - 关于全局内存访问方式

c - scanf() 将换行符保留在缓冲区中

无法使用 FFT 和 FFTW 检索原始图像

c - 为什么 pthread_join 在最后一次迭代中失败(给出段错误)?

c - 使用 ncurses 处理转义序列? printf 是否处理转义序列?

c - pthread 竞争条件,可疑行为