【发布时间】:2021-11-13 06:27:38
【问题描述】:
我在下面尝试了这段代码,但似乎跳过了scanf("%c")。它只要求我输入姓名和年龄,并跳过下面的行。它只是在if 语句上方的printf 中打印文本。有人可以帮忙吗?
#include<stdio.h>
int main()
{
int age;
char sex;
char name[20];
char status;
printf("Enter your last name\n");
scanf("%s", &name);
printf("Enter your age\n");
scanf("%d", &age);
printf("Enter sex (M/F)\n");
scanf("%c", &sex);
printf("your status,married, single,irrelevant (M/S/I)\n");
scanf("%c", &status);
if(age>=16 && sex=='M')
printf("hello, Mr %s\n", name);
if(age<16 && sex =='M')
printf("hello, Master %s\n", name);
if(sex=='F' && status=='M')
printf("hello, Mrs %s\n", name);
if(sex=='F' &&(status=='S' ||status=='I'))
printf("hello,miss %s\n", name);
}
【问题讨论】:
-
除非您对换行符之类的空格感兴趣,否则不要使用
%c。只需使用字符串转换%s并使用输入的第一个字符。