2017-02-09 38 views
0

我写了一些C代码,你可以通过输入1或2来从答案中选择,如果你输入一个更高的数字,它会带你回来选择另一个数字就好了。但是,如果我尝试输入不是值或数字的内容,如字符串或字符,底部的错误消息将无限重复。当输入任何其他字符时,如何让我的代码的行为与输入比1或2更高的数字相同?这里我使用抽象的代码:如何停止制作此C代码循环?

#include <stdio.h> 

int a; 

int main(){ 
    b:scanf("%d", &a); 
    if(a==1) 
    { 
     a=0; 
    } 
    if(a==2) 
    { 
     a=0; 
    } 
    else 
    { 
     a=0; 
     printf("\nERROR: Please try again.\n\n"); 
     goto b; 
    } 
} 

编辑:显然返回值仍然卡在scanf()时,它返回到它。我如何清除scanf()的返回值?

+5

我会使用一个'while'循环这个,而不是goto方法的。然后只是从循环中断开,或者设置一个标志来决定是否继续循环。 goto的合法使用案例远远少于合法用例。 – Carcigenicate

+3

呃...后藤。呸。学习正确的方法来做到这一点*现在*,在为时已晚之前。 –

+2

如果你用'fgets'得到输入,然后在输入字符串中使用'sscanf',当用户没有输入你需要的东西时,你可以忘记以前的输入字符串并重试。 OTOH使用'scanf'时,任何被拒绝的输入都将保留在输入缓冲区中。 **总是**检查'scanf'函数族的返回值(参见手册页了解其含义)。永远不要认为输入数据是*好*。 –

回答

-2

类似...... 注:显然999是一个任意值。只是选择给你一个例子。

#include <stdio.h> 


int main(){ 
int a = 1; 
while (a != 999){ 
    scanf("%d", &a); 
    if(a==1) 
    { 
    a=0; 
    } 
    if(a==2) 
    { 
    a=0; 
    } 
    else if (a != 999) 
    { 
     a=0; 
     printf("\nERROR: Please try again.\n\n"); 
    } 
} // while() 
} // main() 
+3

定义了“b”在哪里?这甚至不会编译,我认为我们不应该宣传'goto'。 – stevieb

+0

您还需要检查'scanf()'的返回值。 –

+0

放松。别紧张。这是因为我复制了初始代码并很快修改了它。 goto不见了。意味着从原始来源删除第二次参考。 另外,从来没有抛出异常,因为这也是一个转到。 :)想一想,抛出异常并且代码直接跳转到错误位置。呃哦,转到。 :) – raddevus

-1
#include <stdio.h> 

int a; 

int isNumeric(const char *str) 
{ 
    while(*str != '\0') 
    { 
     if(*str < '0' || *str > '9') 
      return 0; 
     str++; 
    } 
    return 1; 
} 

int main(){ 

    char inputStr[10]; 
    while(1){ 
     scanf("%9s",inputStr); 
     if(!isNumeric(inputStr)){ 
      a=0; 
      printf("\nERROR Not a number: Please try again.\n\n"); 
     }else { 
      a = atoi(inputStr); 
      if(a==1){ 
       a = 0; 
      }else if(a == 2){ 
       a == 0; 
      }else{ 
       a=0; 
       printf("\nERROR : Please try again.\n\n"); 
      } 
     }`enter code here` 
    } 
} 

没有测试。但我想你会得到一个好主意。检查strtol函数。这也很有用。

+2

也许'scanf(“%9s”,inputStr)'也使用'isdigit' –

+1

...并测试scanf()的返回值以确保它有一个值。 –

+0

完成老板:)。顺便说一句,我猜isdigit需要循环相同的循环次数。所以我希望这不会是一个性能问题。干杯:) – vk3105

-1

请勿使用gotos。取而代之,使用while循环:

#include <stdio.h> 

int main(void) { 
int a, end = 1; //end determines if the loop should end 
do { //a do-while loop - it's the same as a while loop, except it runs atleast once 
    scanf("%d", &a); 
    switch (a) { //switches the value of a 
    case 1: 
    case 2: printf("You entered %d\n", a); 
      end = 0; //sets end to 0, which will end the loop(see below) 
      break; 
    default: printf("\nERROR: Please try again.\n\n"); 
    } 
} while (end); //every non-zero value is true, so when I set end to 0, it will end the loop 
return 0; //don't forget the return 0: it shows you that your program ran without error 
} 

所以我写到它只要输入有效的输入就会结束。您也不需要将a设置为零,因为每次运行循环时都会再次读取它。
编辑:如果你想检查是否有无效的输入,如5x,您可以使用以下命令:

int check, var, error; 
char ch; 
do { 
error = 0; 
check = scanf("%d%c", &var, &ch); 
if (check != 2 || ch != '\n') { 
    printf("Wrong input. Try again -> "); 
    error = 1; 
    fflush(stdin); 
} 
} while (error); 
+1

也许检查'scanf'的返回值将有助于 –

+0

'switch'在if语句会执行时过度 –