怎么检验赋给double a[]的数据为数字而不是字母


如何检验赋给double a[]的数据为数字而不是字母
我想在赋给数组之前检验,要是赋值后检验就没意义了。
有没有什么方法?请指点

C++/VC 程序开发 异常处理

时崎Area 10 years, 12 months ago


int dotnumber = 0;
for (i=0;i <sizeof(a);i++)
{
if ( isdigit(a[i]) )
continue;
if (a[i] == '. ' && dot
number ==0)
{
dot_number =1;
continue;
}
return false;
}
return true;

birdman answered 10 years, 12 months ago


不是,是double a[]的赋值,不能用ascii检验的。我想赋数字,而且很大并非0--9.
------------------------------------------
试一下这样怎么样?

#include <stdio.h>

int main(void)
{
double a[10];
int i = 0 ;

for (int j = 0; j < 10; j++)
{
printf ( "\n请输入第 %d 个数字\n-> ", j+1);
while (scanf ( "%f ", &a[i++]) != 1)
{
printf ( "\n输入有错,请重新输入\n-> ");
while (getchar () != '\n ')
{
NULL;
}
}

}

}

Alvinwx answered 10 years, 12 months ago

Your Answer