gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 15:51:58 25 4
gpt4 key购买 nike

基本上我是一个初学者编码员,这是我写的:

#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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com