基本上我是一個初學者編碼器和這是我寫的: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
的playerGender
。之後它向我展示了關於我的性別的問題?我的錯誤在哪裏?
主要問題是使用'scanf'進行用戶輸入。 – melpomene
儘管你是初學者,但你應該有常識來包含你的完整程序和對其行爲的描述。這是非常好的! –
從一開始就學會不使用goto。 – Unimportant