如何防止scanf失败的无限循环 [英] How to prevent an infinite loop with scanf failure
问题描述
该准则应该能为美元带来零钱,并且效果很好.但是这位教授说,他将在输入随机数和字母的同时输入.它可以很好地处理数字,但是当输入字母时,会出现无限循环吗?
The Code is supposed to make change for a dollar and works fine. but the professor says that he will be enter random numbers along with letters. It works fine with numbers but when a letter is entered an infinite loop will occur any suggestions?
#include <stdio.h>
#include <stdlib.h>
#define amtPaid 1
#define SENTINAL -1
int quarters(int numChange);
int dimes(int numChange);
int nickels(int numChange);
int pennies(int numChange);
int main(void)
{
double amtDue = 0; // how much is paid
while(1){
printf("\nPlease enter the price less than 1 dollar: ");
scanf(" %lg", &amtDue);
int changeReturn = (amtPaid - amtDue) * 100 + 0.5; // convert decimal to whole number
int updated = 0; // remaining change after amt of change
int numberQuarters = quarters(changeReturn); // number of quarters needed
if(changeReturn >= 0 && changeReturn <= 100){ // if changereturn is between 0 and 100 execute code
printf("\nNice!");
printf("\nWe owe you %i cents" , changeReturn);
if(numberQuarters >= 0){ // get and print number of quarters
printf("\nQuarters: %i", numberQuarters);
updated = changeReturn % 25;
}
if(dimes(updated) >= 0){ // get and print number of dimes
printf("\nDimes: %i", dimes(updated));
updated = updated % 10;
}
if(nickels(updated)>= 0){ // get and print number of nickels
printf("\nNickels: %i", nickels(updated));
updated = updated % 5;
}
if(pennies(updated) >= 0){ // get and print number pennies
printf("\nPennies: %i", pennies(updated));
}
}
else if(amtDue == SENTINAL){
break;
}
else {
printf("That does not make sense to me. please type a valid number");
}
printf("\n %g", amtDue);
}
return 0;
}
int quarters(int numChange){
int numQuarters = 0;
numQuarters = numChange / 25;
return numQuarters;
}
int dimes(int numChange){
int numDimes = 0;
numDimes = numChange / 10;
return numDimes;
}
int nickels(numChange){
int numNickels = 0;
numNickels = numChange / 5;
return numNickels;
}
int pennies(numChange){
return numChange;
}
推荐答案
如果提供了除scanf()
格式说明符的预期值以外的不适当值,则scanf()
将失败并且不当值保留在输入缓冲区中,将 feed 提供给下一个scanf()
,只会导致连续失败.在这种情况下,您需要清理输入缓冲区,然后再进行下一个输入.您可以使用类似的
In case an inappropriate value is supplied other than the expected value of the format specifier with scanf()
, the scanf()
will fail and the inappropriate value will remain in the input buffer, providing the feed to next scanf()
, only to cause successive failures. In that case, you need to clean up the input buffer before going for next input. You can use something like
- 检查
scanf()
的返回值
- 万一发生故障,请使用
while( getchar() != '\n' );
清理输入缓冲区.
- check the return value of
scanf()
- In case of failure, use
while( getchar() != '\n' );
to clean the input buffer.
也就是说,int nickels(numChange)
现在在c
中(C99
起)无效.您必须明确地将其设置为int
.
That said, int nickels(numChange)
is now invalid in c
(C99
onwards). You have to make it as int
explicitly.
这篇关于如何防止scanf失败的无限循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!