【发布时间】:2018-03-06 20:41:58
【问题描述】:
我不明白为什么最后一行要求用户输入权重没有被执行。会不会是内存问题?还是我写错了结构?还是我错误地编写了对 scanf() 的调用?还是以上所有?
#include <stdio.h>
struct date{
int month;
int day;
int year;
};
struct healthProfile{
char firstName[20];
char lastName[20];
struct date birthday;
float height; //inches
float weight; //pounds
};
int main(void)
{
struct healthProfile patient1;
printf("%s\t", "Please enter the patient's first name:");
scanf("%s", patient1.firstName);
printf("%s\t", "Please enter the patient's last name:");
scanf("%s", patient1.lastName);
printf("%s\t", "Please enter the date of birth(mm/dd/yyyy)");
scanf("%2i/%2i/%4i", &patient1.birthday.month, &patient1.birthday.day, &patient1.birthday.year);
printf("%s\t", "Please enter the patient's height in inches");
scanf("%.2f", &patient1.height);
printf("%s\t", "Please enter the patient's weight in pounds");
scanf("%.2f", &patient1.weight);
return 0;
}
【问题讨论】:
-
你怎么知道不是?
-
这个程序到底是做什么的?它只是正常退出,崩溃还是其他?
-
是的,正常退出。除了最后一行,我可以输入其他所有内容
-
请澄清您的问题,您的意思是程序不会等待用户输入重量输入。
-
在 scanf 中使用
%d而不是%i。否则如果他们在出生月份写09你会遇到麻烦