菜鸟想问一个C语言的有关问题
初学者想问一个C语言的问题
我写了一个简单的计算平方和的程序,我的目的是如果用户输入的数据类型不对,提示用户重新输入。
但是我测试了一下,如果输入的不是两个整数,会无限输出Enter lower and upper integer limits:Wrong input!
Please input again.
只能手动关闭程序。这是怎么回事啊?找了好久也没找到问题所在
#include<stdio.h>
#include<stdlib.h>
int main(void)
{
int low,up,i,sum;
printf("Enter lower and upper integer limits\n");
while ((scanf_s("%d %d", &low, &up)) != 2)
{
printf("Wrong input!\nPlease input again.\n");
printf("Enter lower and upper integer limits:");
}
for (sum = 0, i = low; i <= up; i++)
{
sum = sum + i*i;
if (i == up)
printf("The sums of the squares from %d to %d is %d\n", low, up, sum);
}
system("pause");
return 0;
}
------解决方案--------------------
fflush(stdin);
我写了一个简单的计算平方和的程序,我的目的是如果用户输入的数据类型不对,提示用户重新输入。
但是我测试了一下,如果输入的不是两个整数,会无限输出Enter lower and upper integer limits:Wrong input!
Please input again.
只能手动关闭程序。这是怎么回事啊?找了好久也没找到问题所在
#include<stdio.h>
#include<stdlib.h>
int main(void)
{
int low,up,i,sum;
printf("Enter lower and upper integer limits\n");
while ((scanf_s("%d %d", &low, &up)) != 2)
{
printf("Wrong input!\nPlease input again.\n");
printf("Enter lower and upper integer limits:");
}
for (sum = 0, i = low; i <= up; i++)
{
sum = sum + i*i;
if (i == up)
printf("The sums of the squares from %d to %d is %d\n", low, up, sum);
}
system("pause");
return 0;
}
------解决方案--------------------
fflush(stdin);