C 如何使此代码工作(isdigit)
因此,我正在创建一个计算三角形面积的程序,我需要它来告诉用户他是否键入了字母或负数,我创建了以下代码: 我需要使用isdigitC 如何使此代码工作(isdigit),c,numbers,C,Numbers,因此,我正在创建一个计算三角形面积的程序,我需要它来告诉用户他是否键入了字母或负数,我创建了以下代码: 我需要使用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
#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;
}
#包括
#包括
#包括
#包括
int main(){
浮子a、b、c;
浮点数s=0,ar1=0,ar2=0;
printf(“通知A方的价值”);
fflush(stdin);
scanf(“%f”、&a);
然而(a首先,根据C11标准,在stdin
上使用fflush
是未定义的行为,尽管在某些实现中定义良好
其次,不能简单地使用isdigit
。一旦%f
看到字符等无效数据,scanf
终止,相应的参数保持不变。此外,对未初始化的变量使用isdigit
会导致未定义的行为
您可以做的是检查scanf
的返回值。如果成功,代码中的所有三个scanf
都返回1
固定代码:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <ctype.h> //Unused header
void flushstdin() //Removes all characters from stdin
{
int c;
while((c = getchar()) != '\n' && c != EOF); //Scan and discard everything until a newline character or EOF
}
int main () {
float a, b, c;
float s=0, ar1=0, ar2=0;
printf("Inform the value of side A\n");
//fflush(stdin); Avoid this to make your code portable
while(scanf("%f",&a) != 1 || a <= 0){
printf("Invalid value\n");
flushstdin(); //Alternative way to flush stdin
}
printf("Inform the value of side B\n");
//fflush(stdin);
while(scanf("%f",&b) != 1 || b <= 0){
printf("Invalid value\n");
flushstdin(); //Alternative way to flush stdin
}
printf("Inform the value of side C\n");
//fflush(stdin);
while(scanf("%f",&c) != 1 || c <= 0){
printf("Invalid value\n");
flushstdin(); //Alternative way to flush stdin
}
s=((a+b+c)/2);
ar1=(s*(s-a)*(s-b)*(s-c));
ar2=pow(ar1,0.5);
printf("The semiperimeter is %f\n", s);
printf("The area of the triangle is %f\n", ar2);
system ("pause");
return 0; // 0 is usually returned for successful termination
}
#包括
#包括
#包括
#包含//未使用的标题
void flushtdin()//从stdin中删除所有字符
{
INTC;
while((c=getchar())!='\n'&&c!=EOF);//扫描并放弃所有内容,直到出现换行符或EOF为止
}
int main(){
浮子a、b、c;
浮点数s=0,ar1=0,ar2=0;
printf(“通知A方的值”);
//fflush(stdin);避免这种情况以使代码可移植
虽然(scanf(“%f”,&a)!=1 | | a,但我也是一个初学者,所以我不知道CRemove中的很多函数,所有这些fflush(stdin)
。在许多实现中都是UB。UB是什么?我删除了它,什么都没有发生。CANF可能不是满足您需求的正确工具。看看哪个解决了同样的问题,并解释为什么scanf不能为您做正确的事情。@RafaelVignoli,UB表示未定义的行为。基本上,它意味着任何事情都可能发生。Googl你会看到它是什么。非常感谢!嗯,但是现在,当我计算面积时,它总是说:“三角形的面积是-1。#IND00这是什么意思?@Cool guy不知道。你的输入是什么?”通知A边的值“2”通知B边的值“2”通知C边的值“5半周长为4.5000000三角形的面积为-1.#IND00@CoolGuy@RafaelVignoli,因为无法构造边长为2,2,5的三角形