繁体   English   中英

用C进行数据验证-确保输入格式正确

[英]Data validation in C - ensuring the input is in a correct format

我要编写代码以确保用户仅输入1位数字。 如果用户输入“ 0 1 3”之类的信息,我希望程序读取一条错误消息,但我不知道该怎么办。 有人知道如何解决这个问题吗? 如果用户输入的数字串之间有一个空格,那么我当前的代码只会采用第一个数字。

请在下面查看我的代码。 感谢:D

//Prompt the user to enter the low radius with data validation
printf("Enter the low radius [0.0..40.0]: ");
do
{   
    ret = scanf("%lf", &lowRadius);
    //type validation
    if (ret != 1)
    {
        int ch = 0;
        while (((ch = getchar()) != EOF) && (ch != '\n'));
        printf("Wrong input. Please enter one numerical value: ");  
    }
    //range validation      
    else if((lowRadius < 0 || lowRadius > 40))
    {
        printf("Incorrect value. Please enter in range 0-40: ");
    }
    else break;
} while ((ret != 1) || (lowRadius < 0 || lowRadius > 40));//end while lowRadius

如果将行读为字符串,然后对其进行分析,则可以避免挂起未提供的输入的问题。 您已经完成了大部分工作,但这显示了如何捕获过多的输入。 它通过扫描double精度字符后的字符串以获取更多输入来工作。 sscanf的返回值告诉您是否存在,因为它返回成功扫描的项目数。

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

void err(char *message)
{
    puts(message);
    exit(1);
}

int main(void)
{
    double lowRadius = 0.0;
    char inp[100];
    char more[2];
    int conv;
    if(fgets(inp, sizeof inp, stdin) == NULL) {
        err("Input unsuccesful");
    }
    conv = sscanf(inp, "%lf %1s", &lowRadius, more);  // conv is number of items scanned
    if(conv != 1) {
        err("One input value is required");
    }
    if(lowRadius < 0.0 || lowRadius > 40.0) {
        err("Number out of range");
    }
    printf("%f\n", lowRadius);
    return 0;
}

我不确定您指定的位数是多少,因为这将不允许输入最大值。

阅读一整行并用strtod转换。

亚历山大(Alexander)拥有正确的方法,但没有提供太多细节。 这是我的方法,使用getline()读取输入,然后使用strspn()和strtod()解析读取的输入。 如果您不熟悉使用指针,这将很难理解-但是,如果您正在学习C,最终将到达那里:

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

int main(void) {
  double lowRadius;
  char *lineptr = NULL;
  size_t n;
  char *startptr;
  char *endptr;
  char *ws = " \t\n"; /* possible whitespace characters */

  printf("Enter the low radius [0.0..40.0]: ");

  while(1) {
    /* free lineptr if set - neeeded if we iterate on error input */
    if( lineptr ) {
      free(lineptr);
      lineptr = NULL;
    }

    /* now read a line of input */
    while( getline(&lineptr, &n, stdin) == -1 ) {
      /* error returned, just retry */
      continue;
    }

    /* skip over any leading whitespace */
    startptr = lineptr + strspn(lineptr,ws);

    /* Now try to convert double */
    lowRadius = strtod(startptr, &endptr);

    if( endptr==startptr || endptr[strspn(endptr,ws)] != 0 ) {
      /* either no characters were processed - e.g., the
         line was empty, or there was some non-whitespace
         character found after the number. */
      printf( "Wrong input.  Please enter one numerical value: ");
    } else if( (lowRadius < 0.0) || (lowRadius > 40.0) ) {
      printf( "Incorrect value. Please enter in range 0-40: " );
    } else {
      if( lineptr ) free(lineptr);
      break;
    }
  }
  printf( "value entered was %lf\n", lowRadius );
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM