所以,我創建了一個計算三角形面積的程序,我需要它告訴用戶他是否鍵入了一個字母或負數,以便我創建的代碼: 我需要使用ISDIGIT如何使此代碼工作(isdigit)
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <ctype.h>
int main() {
float a, b, c;
float s=0, ar1=0, ar2=0;
printf("Inform the value of side A.");
fflush(stdin);
scanf("%f",&a);
while(a<=0||isdigit((int)a)){
printf("Invalid value.");
fflush(stdin);
scanf("%f",&a);
}printf("Inform the value of side B.");
fflush(stdin);
scanf("%f",&b);
while(b<=0||isdigit((int)a)){
printf("Invalid value.");
fflush(stdin);
scanf("%f",&b);
}printf("Inform the value of side C.");
fflush(stdin);
scanf("%f",&c);
while(c<=0||isdigit((int)a)){
printf("Invalid value.");
fflush(stdin);
scanf("%f",&c);}
s=((a+b+c)/2);
ar1=(s*(s-a)*(s-b)*(s-c));
ar2=pow(ar1,0.5);
printf("The semiperimeter is %f",s);
printf("The area of the triangle is%f",ar2);
system ("pause");
return 1;
}
但是,當我編譯/運行,輸入「X」,或當我本來是要鍵入數字「布拉布拉」,沒有任何反應,和程序不警告我,我該怎麼辦?
另外,我是一個初學者,所以我不知道有多少功能,用C –
刪除所有這些'fflush(標準輸入)'。在很多實現中都是UB。 –
什麼是UB?我刪除它,什麼也沒有發生 –