【问题标题】:How do I only accept numbers in C and block the user from entering letters and special characters?如何只接受 C 中的数字并阻止用户输入字母和特殊字符?
【发布时间】: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


    【解决方案1】:

    您不能阻止用户输入他或她想要的任何内容,但您可以使用scanf 的返回值来确定是否输入了有效值,并提示用户输入正确的内容:

    float base;
    do {
        printf("Please enter the value of base of the triangle: \n");
    } while (scanf ("%f", &base) != 1 || base < .5 || base > 100);
    

    这个循环会一直持续到满足所有三个条件:

    • scanf 只返回了一项,
    • scanf提供的值大于等于0.5,且
    • scanf提供的值小于等于100

    【讨论】:

    • OP 想要“给出错误是(如果)用户输入任何不是数字的任何内容”的弱点。这不会完全消耗像“123abc”这样的非数字文本,而是将“abc”留在stdin 中并且不会出现错误。建议fgets() 然后sscanf()/strtof
    【解决方案2】:

    scanf 返回已成功匹配并填充值的变量数。 做吧:

    int result = scanf ("%f", &base);
    if(result != 1)
    {
        ...//handle error
    }
    

    【讨论】:

      【解决方案3】:

      我认为您可以逐个字符地读取输入的字符,然后检查它是否是数字,如果不是,则显示错误消息。

      #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");
      char c='\n';
      char success=0;
      base=0;
      char dot=0;
      do{
      scanf ("%c", &c);
      if((c>=48)&&(c<=57)){
        if(dot==0)
          base=base*10+(c-48);
        else{
          base+=(c-48)/dot;
          dot*=10;
        }
      }
      else
       if((c=='.')&&(dot==0))
           dot=10;
       else
         if(c!='\n')
           success=1;
      
      }while((c!='\n')&&(succes==0));
      
      
      if(success!=0) return -1; //error we break out
      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");
      c='\n';
       success=0;
      height=0;
       dot=0;
      do{
      scanf ("%c", &c);
      if((c>=48)&&(c<=57)){
        if(dot==0)
          height=height*10+(c-48);
        else{
          height+=(c-48)/dot;
          dot*=10;
        }
      }
      else
       if((c=='.')&&(dot==0))//check if is the first time the user insert a dot
           dot=10;
       else
         if(c!='\n')
           success=1;
      
      }while((c!='\n')&&(succes==0));
      
      
      if(success!=0) return -1; //error we break out
      
      
      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;
      
      
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-03-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-12-15
        相关资源
        最近更新 更多