Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ssis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 检查输入是否为数字_C - Fatal编程技术网

C 检查输入是否为数字

C 检查输入是否为数字,c,C,我想检查用户的输入,如果他的输入是浮点数或整数,那么接受它并对其进行一些数学运算,否则,如果他的输入是字符、符号、大数或除数字以外的任何其他输入,请他输入另一个输入。 提示:我正在为“p.burst\u time”使用数据类型float。 但每次我输入任何输入时,程序都会认为它是错误的输入,因为检查器变为=1,即使输入是正确的&我不知道为什么。 先谢谢你 for(;;){ int i,checker,point=0; char c[25]; c[strlen(c)-1]

我想检查用户的输入,如果他的输入是浮点数或整数,那么接受它并对其进行一些数学运算,否则,如果他的输入是字符、符号、大数或除数字以外的任何其他输入,请他输入另一个输入。 提示:我正在为“p.burst\u time”使用数据类型float。 但每次我输入任何输入时,程序都会认为它是错误的输入,因为检查器变为=1,即使输入是正确的&我不知道为什么。 先谢谢你

for(;;){    
int i,checker,point=0;
    char c[25];
    c[strlen(c)-1] = '\0';
printf("\nEnter The Process's Burst Time > 0 : ");
fgets(c, 21, stdin); //Max input is 20.
fflush(stdin);
for(i=0;i<strlen(c);i++) //strlen(c) is the size. Max is 20.
    {

       if(c[i]=='.')
            point++;
       else if(!isdigit(c[i])){
            checker=1; //Checker is 1 when input isn't a digit.
            break;
       }

    }
    printf("checker = %d\npoint = %d\n",checker,point);
    if(checker==1||point>1){
        printf("\a\aPlease enter a positive number only greater than zero.\n"); //Input has space, symbols, letters. Anything but digits.
        continue;
    }
    else
    {
        p.burst_time = atof(c); //Converting to float only if input is nothing but digits.
        if(p.burst_time<=0)
            continue;
        else
            break;
    }
   }
(;;){ int i,方格,点=0; charc[25]; c[strlen(c)-1]='\0'; printf(“\n输入进程的突发时间>0:”; fgets(c,21,stdin);//最大输入为20。 fflush(stdin); 对于(i=0;i一些问题:

  • 您从未将checker设置为0(仅点),因此在出现第一个错误后,所有输入都将被拒绝
  • fgets为您提供了
    \n
    :您应该忽略它以及初始和终端空白(
    '
    \r
    \t
    \f
    ,至少前2个)
  • 输入流上的fflush是非标准的,当您使用fgets时,它是无用的
  • c[strlen(c)-1]='\0';
    没有意义:您试图放置一个null来终止数组,但是strlen只给出第一个null的位置,这在一个统一数组上是不精确的(感谢WhozCraig注意到了这一点):您可以执行
    c[0]='\0';
    c[sizeof(c)-1]='\0';
    但在这里它同样是无用的

我没有尝试运行它,所以我不知道是否还有其他…

您应该使用
strtod
或相关函数
strtof

#include<stdio.h>
#include <stdlib.h>

int main() {
    double d;
    char c[25];
    char * converted;
    for(;;){
        printf("\nEnter The Process's Burst Time > 0 : ");
        fgets(c, 21, stdin); //Max input is 20.                                                                                     
        d = strtod(c,&converted);
        if (converted == c){
            printf("Conversion unsuccesful");
        }
        else {
            printf("Converted value: %f",d);
        }
    }
}
如果endptr不为NULL,则为指向最后一个字符之后的字符的指针 转换中使用的字符存储在引用的位置 通过endptr

如果未执行任何转换,则返回零,并返回nptr的值 存储在endptr引用的位置


另外,我会使用
getline
而不是
gets
,以避免在输入流太大的情况下出现问题。

请提供一段可执行代码,包括
isdigit
函数和
p
的声明。这是一个练习,还是可以使用标准函数的组合,例如
strtod
stroull
?将
c[strlen(c)-1]='\0';
添加到不适中。
c[]
的内容是不确定的(它是在前一行中逐字声明的)。因此
strlen(c)
也是不确定的。
   double strtod(const char *nptr, char **endptr);