如何正确读取整数?
How to correctly read an integer?
我的程序测试一个数是否是 2 的幂,对正整数很有效。但是当我输入像 5.4 这样的实数时,c=getchar()
行不会等待我的输入。
int main() {
int num;
char c = 'y';
printf("\n\n");
do{
printf("**********************************************\n");
printf("Enter a positive integer to test: ");
scanf("%d",&num);
getchar();
if(num<0) {
printf("cannot accept a negative integer.\n");
}
else
is_power_of_two(num);
printf("Do you want to try again?\nEnter 'y' if yes, else press any other key to exit: ");
c = getchar();
printf("**********************************************\n");
}while(c=='y');
return 0;
}
输出:
Enter a positive number to test: 8
Yes it is a power of 2
Do you want to try again?
Enter 'y' if yes, else press any other key to exit: y
**********************************************
**********************************************
Enter a positive number to test: 7.6
No it is not a power of 2
Do you want to try again?
Enter 'y' if yes, else press any other key to exit: **********************************************
我试过使用 fgets 和 atoi 代替 scanf 和 getchar(),如下所示。但是 fgets 在第二次迭代期间不会等待。我尝试在每次迭代中清除 numbuff。但没有区别。 getchar() 是否也像 scanf 一样将 \n 留在缓冲区中?
这是怎么回事?有没有一种简单或正确的方法可以读取整数而不会造成很多麻烦?
int main() {
char numbuf[10];
int num;
char c = 'y';
printf("\n\n");
do{
printf("**********************************************\n");
printf("Enter a positive number to test: ");
fgets(numbuf, sizeof(numbuf),stdin);
num = atoi(numbuf);
if(num<0) {
printf("cannot accept a negative integer.\n");
}
else
is_power_of_two(num);
printf("Do you want to try again?\nEnter 'y' if yes, else press any other key to exit: ");
c = getchar();
printf("**********************************************\n");
}while(c=='y');
return 0;
}
输出:
**********************************************
Enter a positive number to test: 9
No it is not a power of 2
Do you want to try again?
Enter 'y' if yes, else press any other key to exit: y
**********************************************
**********************************************
Enter a positive number to test: No it is not a power of 2
Do you want to try again?
Enter 'y' if yes, else press any other key to exit:
我调试了你的代码,在里面看到一个有趣的事情:当你输入一个浮点数比如5.4时,5保存在num中,剩下4,所以当它被要求输入一个'y'或者按任意键,扫描到“4”,程序终止。
看图(我输入的是5.4)
我想如果你将 num 定义为 float 类型,你的问题就解决了。
我的程序测试一个数是否是 2 的幂,对正整数很有效。但是当我输入像 5.4 这样的实数时,c=getchar()
行不会等待我的输入。
int main() {
int num;
char c = 'y';
printf("\n\n");
do{
printf("**********************************************\n");
printf("Enter a positive integer to test: ");
scanf("%d",&num);
getchar();
if(num<0) {
printf("cannot accept a negative integer.\n");
}
else
is_power_of_two(num);
printf("Do you want to try again?\nEnter 'y' if yes, else press any other key to exit: ");
c = getchar();
printf("**********************************************\n");
}while(c=='y');
return 0;
}
输出:
Enter a positive number to test: 8
Yes it is a power of 2
Do you want to try again?
Enter 'y' if yes, else press any other key to exit: y
**********************************************
**********************************************
Enter a positive number to test: 7.6
No it is not a power of 2
Do you want to try again?
Enter 'y' if yes, else press any other key to exit: **********************************************
我试过使用 fgets 和 atoi 代替 scanf 和 getchar(),如下所示。但是 fgets 在第二次迭代期间不会等待。我尝试在每次迭代中清除 numbuff。但没有区别。 getchar() 是否也像 scanf 一样将 \n 留在缓冲区中?
这是怎么回事?有没有一种简单或正确的方法可以读取整数而不会造成很多麻烦?
int main() {
char numbuf[10];
int num;
char c = 'y';
printf("\n\n");
do{
printf("**********************************************\n");
printf("Enter a positive number to test: ");
fgets(numbuf, sizeof(numbuf),stdin);
num = atoi(numbuf);
if(num<0) {
printf("cannot accept a negative integer.\n");
}
else
is_power_of_two(num);
printf("Do you want to try again?\nEnter 'y' if yes, else press any other key to exit: ");
c = getchar();
printf("**********************************************\n");
}while(c=='y');
return 0;
}
输出:
**********************************************
Enter a positive number to test: 9
No it is not a power of 2
Do you want to try again?
Enter 'y' if yes, else press any other key to exit: y
**********************************************
**********************************************
Enter a positive number to test: No it is not a power of 2
Do you want to try again?
Enter 'y' if yes, else press any other key to exit:
我调试了你的代码,在里面看到一个有趣的事情:当你输入一个浮点数比如5.4时,5保存在num中,剩下4,所以当它被要求输入一个'y'或者按任意键,扫描到“4”,程序终止。
看图(我输入的是5.4)
我想如果你将 num 定义为 float 类型,你的问题就解决了。