停留在一个无限循环输入查询的退出条件后。 [英] Stuck in a infinite loop after inputing the exit condition.

查看:138
本文介绍了停留在一个无限循环输入查询的退出条件后。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使这个运行在一个循环,直到一个非数字输入中给出。问题是,当我输入一个字母退出while循环,它进入一个无限循环。它也进入第一个if语句,并通过不断循环。如果有人对如何解决这一问题的任何想法,这将是巨大的。

  INT计数器;
INT输入[100]
INT NUM = 1
而(输入[NUM] == 0)
{
    的printf(#得分%D:,计数器);
    scanf函数(%d个,&安培;输入[NUM]);    如果(输入[NUM]< = 0){
        的printf(你不能用负数\\ n);
        继续;
    }
    如果(输入[NUM]> = 100){
        的printf(你不能用数字大于100 \\ n);
        继续;
    }
    NUM ++;
    反++;}


解决方案

首先, NUM 应该是0作为数组索引从0而不是1 <启动/ p>

然后,你有输入[NUM] == 0 ,而的条件。你测试使用的是未初始化的变量为输入尚未初始化。这是计数

相同

您code不能编译,因为你已经错过了; 在2号线和3月底

最后,更换您的 scanf函数通过以下code:

 如果(scanf函数(%d个,&安培;输入[NUM])== 0)
{printf的(非数字字符输入.Exiting环... \\ n);
scanf函数(%* S); // asterick告诉scanf函数放弃扫描的字符串(清除输入的字符)
打破;
}

所以最后,修改code:

  INT计数器= 1; //初始化变量
INT输入[100]; //这里分号
INT NUM = 0; // NUM必须为0
而(1)//死循环
{
    的printf(#得分%D:,计数器);    如果(scanf函数(%d个,&安培;输入[NUM])== 0)//如果没有值扫描
{printf的(非数字字符输入.Exiting环... \\ n);
scanf函数(%* S); // asterick告诉scanf函数放弃扫描的字符串(清除输入的字符)
打破;
}    如果(输入[NUM]&LT; = 0)
        的printf(你不能用负数\\ n);    否则,如果(输入[NUM]&GT; = 100)
        的printf(你不能用数字大于100 \\ n);
   其他{
    NUM ++;
    反++;}}

I am trying to make this run in a loop until a non-numeric input is given. The problem is that when I input a letter to exit the while loop, it goes into an infinite loop. It also goes into the first if statement and continues to loop through that. If someone has any ideas on how to fix this, it would be great.

int counter;
int input[100]
int num = 1
while (input[num] == 0)
{
    printf("score #%d:", counter);
    scanf("%d",&input[num]);

    if (input[num] <= 0){
        printf("you cannot use negative numbers\n");
        continue;
    }
    if (input[num] >= 100){
        printf("you cannot use numbers greater than 100\n");
        continue;
    }
    num++;
    counter++;

}

解决方案

First of all,num is supposed to be 0 as array index starts from 0 not 1.

Then,you have input[num]==0 in the condition in the while . You test using an uninitialized variable as input has not been initialized. This is the same for counter.

Your code won't compile as you have missed the ; at the end of line 2 and 3.

Lastly,replace your scanf with the following code:

if(scanf("%d",&input[num])==0)
{printf("non-numeric character entered .Exiting loop...\n");
scanf("%*s");// asterick tells scanf to discard scanned string(clear the entered char)
break;
}

So finally,the modified code:

int counter=1; // initialize variable
int input[100]; //semicolon here
int num = 0; //num must be 0
while (1) //infinite loop
{
    printf("score #%d:", counter);

    if(scanf("%d",&input[num])==0) //if no value is scanned
{printf("non-numeric character entered .Exiting loop...\n");
scanf("%*s");// asterick tells scanf to discard scanned string(clear the entered char)
break;
}

    if (input[num] <= 0 )
        printf("you cannot use negative numbers\n");

    else if (input[num] >= 100)
        printf("you cannot use numbers greater than 100\n");
   else{
    num++;
    counter++;}

}

这篇关于停留在一个无限循环输入查询的退出条件后。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆