我正在尝试编写一个小程序,将一个人或者任何高度从英制转换为公制,但我得到的编译器错误告诉我:"数据参数未被格式化使用字符串"
printf("Enter your height with just a space between feet and inches: ");
scanf("%2s", "%f", &ft, &in);
ft = in / 12;
double delta = (ft * 30.48);
double rem = in * 2.54;
double calc = delta + rem;
printf("Your height is %f ft and %.1f in\n", delta, calc);
return (0);
答案 0 :(得分:0)
您的代码中存在很多错误。
ft
(英尺)通常是一个浮动变量,使用%f
代替%s
。还要在"%f %f"
之类的单引号中给出所有格式说明符。可能你想要这样
scanf("%2f %f", &ft, &in);
您应该通过检查scanf()
的返回值检查scanf()
是否成功,通过在命令提示符下键入man 3 scanf
阅读手册页或参见{{3 }。例如
int ret = scanf("%2f %f", &ft, &in);
以下是示例代码
#include<stdio.h>
int main(void) {
float ft = 0, in = 0; /* don't keep uninitialized local variable */
printf("Enter your height with just a space between feet and inches: ");
scanf("%2f %f", &ft, &in);
ft = in / 12;
double delta = (ft * 30.48);
double rem = in * 2.54;
double calc = delta + rem;
printf("Your height is %f ft and %.1f in\n", delta, calc);
return 0;
}
同样使用-Wall
标记gcc -Wall test.c
标记您的程序,不要忽略警告,解决它们。通过使用-Wstrict-prototypes -Werror
进行编译,可以更好地将所有警告视为错误,从而减少错误的可能性。例如
gcc -Wall -Wstrict-prototypes -Werror test.c
最后学习如何调试小代码http://man7.org/linux/man-pages/man3/scanf.3.html