我試圖做一個有條件的和的if-else這個問題但是我的主要功能是不執行的我本來打算順序步驟循環 。如何讓我的「主」功能以正確的順序執行?
#include<stdio.h>
void draft(int, char);
void main()
{
int age = 0;
char sex;
printf("How old are you?");
scanf_s("%d", &age);
printf("Please enter your sex.(M or F)");
scanf_s("%c", &sex);
draft(age,sex);
system("pause");
return;
}
void draft(int age,char sex)
{
if (age>= 21 && sex=="M")
{
printf("Congratulations son, You will be going off to Syria to fight for your country.\n");
}
else if (age >= 18 && sex == "M")
{
printf("Congratulations son, You will be going off to Vietnam to fight for your country.\n");
}
else if (age < 18 && sex == "M")
{
printf("Sorry Son you're still too young.\n");
}
else if (age >= 21 && sex == "F")
{
printf("Sorry,miss, only men can serve.\n");
}
else if (age >= 18 && sex == "F")
{
printf("Sorry,little lady, only men can serve.\n");
}
else if (age < 18 && sex == "F")
{
printf("Sorry,little girl, only men can serve.\n");
}
else
{
printf("Please enter age and sex.");
}
return;
}
將提示用戶輸入他的年齡,但進入正題後,將直接進入「徵求意見稿」函數的最後一個ELSE語句不給用戶輸入性的機會。
我不明白基於這裏的代碼會發生什麼。在調試器中逐行執行。 –
@EricJ .:我猜你沒有看到'scanf_s(「%c」,&sex);'? – EOF
如果你只是使用'scanf'會怎麼樣? –