【发布时间】:2012-09-23 19:13:39
【问题描述】:
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
int main(void)
{
char wunit[2]; // weight unit
char hunit[2]; // height unit
double weight, height;
printf("Enter the body weight: ");
scanf("%lf%s", &weight, &wunit); // input weight and unit eg. 150lb
printf("Enter the height: ");
scanf("%lf%s", &height, &hunit); // input height and unit eg. 5.65 ft
printf("The height unit: %s\n", hunit);
printf("The weight unit: %s", wunit);
return 0;
}
此代码仅打印出身高单位,而不是体重单位。我能做些什么来解决它?
【问题讨论】:
-
快速提问。如果您正在编写 C 代码,为什么要标记为 C++?
-
printf期望带有%s的字符串输出以空值结尾。将数组的长度增加到至少 3,并使用scanf("%2s", hunit)告诉scanf不要读取超过两个字符。 -
@AnthonyBurleigh 安全功能是否标准化?我以为它们只是 VC++ 的东西……我猜想学习一些新东西。
-
@Craig 我是一名 C++ 程序员,而不是 C,抱歉这个错误!我已编辑我的评论以使用标准
scanf而不是推荐scanf_s。谢谢! -
为什么不在重量后打印
\n?