【发布时间】:2014-09-29 18:34:33
【问题描述】:
我只需要弄清楚如何在用户输入任何不是数字的内容时给出错误。我已经设置了无法通过或无法通过的代码的值。
我只需要接受数字:如果输入了字母或任何类型的特殊字符,我希望程序自行取消。我该怎么做?
#include <stdio.h>
#include <math.h>
int main(void) {
float base, height;
float area;
printf("Please enter the value of base of the triangle: \n");
scanf ("%f", &base);
if(base<.5)
printf("Invalid Input\n");
while (base<.5)
return 0;
if(base>100)
printf("Invalid Input\n");
while(base>100)
return 0;
printf("Please enter the value of height of the triangle:\n");
scanf("%f", &height);
if(height<1)
printf("Invalid Input\n");
while(height<1)
return 0;
if(height>75)
printf("Invalid Input\n");
while (height>75)
return 0;
area = base * height/2;
printf("The area of the triangle for base: %f and height: %f is %f \n", base,
height , area );
return 0;
}
【问题讨论】:
标签: c validation input