如果我将char值作为输入而不是int,它为什么会变成无限循环? [英] Why it turns to be an infinite loop if I give a char value as input instead of int?

查看:91
本文介绍了如果我将char值作为输入而不是int,它为什么会变成无限循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include "stdafx.h"
#include<stdio.h>


int main()
{
	unsigned int  i, k = 0, l = 0, m = 0, j = 1;
	int x;
	do
	{
		printf("\nEnter the number of elements you want to enter (MAX VALUE 100) : ");
		scanf_s("%d,", &x);
		if (x >= 0&&x<=100)
		{
			int a[100];
			for (i = 1; i <= x; i++)
			{
				printf("\nEnter the %d element : ", i);
				scanf_s("%d", &a[i]);
				if (a[i] > 0)
					++k;
				if (a[i] < 0)
					++l;
				if (a[i] == 0)
					++m;
				j++;
			}
		}
		else
			printf("\nInvalid entry");
	} while (j <= 1);
		printf("\nNo of Positive numbers are : %d\nNo of Negative numbers are : %d\nNo of zeros are : %d\n", k, l, m);
	

	return 0;
}



上述程序只计算用户输入的数字序列中的正数,负数和零数。我已经使用do while循环进行了一个小的检查,无论用户给出的最大范围是否是有效数字,但是如果用户输入一个字符或一个特殊符号怎么办(好吧我自己尝试了它,结果证明是无限的loop)如何执行检查以避免程序进入无限循环,并且我想知道为什么它在输入字符或符号时进入无限循环。



我尝试了什么:



我试过输入一个字符/符号作为输入它给出了一个输出如下无限次。

输入您要输入的元素数量:$

无效条目

输入您想要的元素数量输入:

无效条目

输入您要输入的元素数量:

无效条目





TENDS TO INFINITE


The above program simply counts the number of positives, negatives and zeros in sequence of numbers entered by the user. I have put a small check using do while loop whether the max range given by the user is a valid number or not, But what if the user enters a character or a special symbol(Well I tried it myself it turns out to be an infinite loop)how to perform a check for it in order to avoid the program getting into the infinite loop and also I want to know why it is entering the infinite loop upon entering a character or a symbol.

What I have tried:

I have tried entering a character/symbol as an input it gives an output like the following infinite times.
Enter the number of elements you want to enter : $
Invalid entry
Enter the number of elements you want to enter :
Invalid entry
Enter the number of elements you want to enter :
Invalid entry
.
.
TENDS TO INFINITE

推荐答案



无效条目

输入您要输入的元素数量:

无效条目

输入您要输入的元素数量:

无效条目< br $> b $ b。



TENDS TO INFINITE

Invalid entry
Enter the number of elements you want to enter :
Invalid entry
Enter the number of elements you want to enter :
Invalid entry
.
.
TENDS TO INFINITE


j 永远不会小于1,因此循环不会退出。 j 仅递增: j ++



问你自己,用户如何告诉我他们已经完成了?。



而不是随着你的计算,在用户完成后这样做会更好输入他们的号码。



这是伪代码,你可以做你想做的事:

j is never less than 1, so the loop won't exit. j is only incremented: j++.

Ask yourself, "How does the user tell me they are finished?".

Rather than calculate as you go, it would be better to do that after the user has completed entering their number.

Here is pseudo code to do what you want:
do
{
    /* 1. get user input
     * 2. test input:
     *     if {exit loop marker} then break
     *     if {invalid value} then warn
     *     else add value to array
     */
}
for {start; test max; increment}
{
    /* if isPositive, inc positive count
     * if isNegative, inc negative count
     * else inc zero count
     */
}
// report counts back to user



这应该足以帮助您编写代码。



提示:使用表示值所包含的变量名称。使代码的可读性提高1000%。


This should be enough of a guide to help you write your code.

TIP: use variable names that represent what the values hold. Make code 1000% more readable.


这篇关于如果我将char值作为输入而不是int,它为什么会变成无限循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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