2015-09-26 99 views
0

我是新来C编程,我想检查我的所有数组元素是除第一个元素以外的整数。无限循环时输入无效输入

我写了下面的代码,但是一旦插入错误的输入,循环就不会停止。

bool validate_int(char input[]){ 
    fgets(input,10, stdin); 
    for(int i = 1; i < strlen(input); ++i) { 
     if(!isdigit(input[i])){ 
      i = 1; 
      fgets(input,10, stdin); 
     } 
     else{ 
     } 
    } 
    return true; 
} 
+3

你绝不能忽略的输入操作的返回值。你的方法被打破,无法修复。 –

+0

@KerrekSB呃......只有在错误检查是需求的情况下。我听到人们说了很多,但严重的是,这可能是一个用于学习字符串操作的小型玩具程序,而不是用于了解处理I/O错误的学习。 – immibis

+0

我只想检查从索引1开始的数组元素是不是其他类型的整数。 –

回答

2

您的代码有一些小问题。这里是一个更好的办法来做到这一点(未经测试)

bool validate_int(char input[]) /* Bad function name; See @Filipe's comment */ 
{ 
    for(;;) /* Infinite loop */ 
    { 
     if(fgets(input, 10, stdin) == NULL) /* If fgets failed */ 
     { 
      puts("fgets failed"); 
      return false; 
     } 

     int i, len = strlen(input); 

     if(len > 0 && input[len - 1] == '\n') /* If there is a newline character at the end of input */ 
      input[--len] = '\0'; /* Replace the '\n' with '\0' and decrement len */ 

     if(!isalpha(input[0])) /* If the first character of input is not an alphabet */ 
      continue; /* Loop again */ 

     if(len == 1) /* There is no number */ 
      continue; 

     for(i = 1; i < len; ++i) 
     { 
      if(!isdigit(input[i])) /* If not a digit */ 
       continue; /* Loop again */ 
     } 

     break; /* Get out of the loop */ 
    } 

    return true; 
} 

更更好的方式是独立的输入和验证分为两个独立的功能(未经测试)

bool getInput(char input[]) 
{ 
    if(fgets(input, 10, stdin) == NULL) /* If fgets failed */ 
    { 
     puts("fgets failed"); 
     return false; 
    } 

    int len = strlen(input); 

    if(len > 0 && input[len - 1] == '\n') /* If there is a newline character at the end of input */ 
     input[--len] = '\0'; /* Replace the '\n' with '\0' and decrement len */ 

    return true; 
} 

bool validate(char input[]) 
{ 
    if(!isalpha(input[0])) /* If the first character of input is not an alphabet */ 
     return false; 

    int i, len = strlen(input); 

    if(len == 1) /* There is no number after the character */ 
     return false; 

    for(i = 1; i < len; ++i) 
    { 
     if(!isdigit(input[i])) /* If not a digit */ 
      return false; 
    } 

    return true; 
} 

和在调用功能(再次,未经测试)

char input[10]; 
if(getInput(input)) 
{ 
    if(validate(input)) 
    { 
     puts("Input is in correct format"); 
    } 
    else 
    { 
     puts("Input is in wrong format"); 
    } 
} 
else 
{ 
    puts("Failed to get input"); 
} 
+0

我想你多次调用'strlen'。我认为''len'应该在调用'fgets'并检查其返回值之后计算('int len = strlen(input);')。 i''len'应该用于检查换行符,并且如果删除换行符,应该调整(递减)。 – MikeCAT

+0

谢谢。编辑。希望它看起来不错。 –

+0

我们不应该向OP展示这样做的好方法吗?通过分隔验证输入? – Cubia

1

试试这个

bool validate_int(char input[]){ 
    bool valid; 

    do{ 
     valid = false; 
     fgets(input,10, stdin); 
     for(int i = 1; input[i] && input[i] != '\n'; ++i) { 
      if(!isdigit(input[i])){ 
       valid = false; 
       break; 
      } else { 
       valid = true; 
      } 
     } 
    }while(!valid); 
    return true; 
} 
+0

@感谢很多家伙。 –

2

这里是另一种方法,我会采取,这是必须清洁IMO:

这是你的验证功能:

bool customValidation(char *string) 
{ 
    int len = strlen(string); 
    if (!isalpha(string[0]) || (len > 1 && !isdigit(string[1]))) 
     return false; 

    for (int i = 1; i < len && string[i] != '\n'; ++i) 
     if (!isdigit(string[i])) 
      return false; 

    return true; 
} 

这是你将如何使用它:

char input[10]; 
do 
{ 
    fgets(input, 10, stdin); 
} while (!customValidation(input)); 

显然你应该重命名customValidation()为更重要的东西。

+0

@CoolGuy自从我上次使用fgets()之后过了一段时间,我认为我们需要添加一个额外的char来容纳'\ 0'。编辑。 – Cubia

+0

我认为当输入有一个数字时,你的代码不能正常工作,例如:'A \ n' –

+0

@CoolGuy增加了验证,因此如果第一个位置没有数字,它就会失败。 – Cubia

-1

检查:

int a_Length = 10; 
char input[a_Length]; 
fgets(input,a_Length, stdin); 
for(int i = 1; i < strlen(input); ++i) { 
    if(!isdigit(input[i])&& input[i]!='\n'){ 
     i = 0; 
     printf("Again try: "); 
     if (input[a_Length - 1 ]!='\n') 
      getchar(); 
     fgets(input,10, stdin); 
    } 
} 
+0

1)问题是关于C,而不是C++ 2)'fgets'不会在标准输入流中保留换行符。它将它消耗并存储在'input'中(假设'input'中有空格)。 –

+0

你能运行'fgets(input,10,stdin); char c = getchar();'?解释为什么char c ='\ n'会得到?@ CoolGuy –

+0

什么是输入?它是否是9个字符,不包括'\ n'? –