[英]scanf() doesn't prompt input in stdin
#include <stdio.h>
int main(void) {
float base, height, hyp;
printf("input base of triangle:\n");
scanf("%f", base);
printf("input height of triangle:\n");
scanf("%f", height);
printf("input hypotenuse of triangle:\n");
scanf("%f", hyp);
float perimeter = base + height + hyp;
printf("the perimeter of your triangle is: %f\n", perimeter);
return 0;
}
我正在ideone.com上运行它,它显示成功,然后标准输入为空,然后在stdout中它显示我所有不带数字的打印语句
scanf
需要一个指向您的数据类型的指针,您应该使用&
传递变量的地址:
scanf("%f", &base);
scanf("%f", &height);
scanf("%f", &hyp);
另外,一些错误检查可能很有用,例如:
if(scanf("%f", &base) != 1) //number of items scanned is expected to be 1
//process error..
//etc
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.