帮忙找下错误!谢谢了!~
#include<stdio.h>
void get_choice();
int fun (int,int);
int main(void)
{
int a,b,m;
printf("Please input two number:\n");
scanf("%d%d",&a,&b);
m=fun(a,b);
printf("the number you input is %d and it is ok!\n",m);
return 0;
}
void get_choice()
{
printf("Please choose one of the following\n");
printf("1) copy files 2) move files\n");
printf("3) remove files 4) quit\n");
printf("Enter the number of your choice\n");
}
int fun(int min,int max)
{
printf("%d %d\n",min,max);
int m,n;
getchar(); //用于清除上次scanf()输入时缓冲区里残留的换行符
n=scanf("%d",&m);
while(n!=1||n<min||n>max)
{
get_choice();
n=scanf("%d",&m);
}
return m;
}
这个程序的意思如下:从键盘键入两个数作为下界和上界(当然这里我就直接默认min是下界,max是上界,输入是也先输入小的数字,在输入大的数字,这是为了简化),然后我在输入一个数字,如果这个数字在min,和max之间,就返回这个数字,反之,就调用get_choice()函数来打印菜单!问题来了,如果我输入了在min和max 之间的数字,就会返回这个数,但我输入的数字不符合要求时,并没有调用get_choice()函数!还是照样的返回了这个不符合要求的输入!请问是什么原因,大家指点一下!
[解决办法]
n=scanf("%d",&m); 天哪 n是scanf函数的返回值 不是你输入的m 还有你的while(n!=1||n<min||n>max)
{
get_choice();
n=scanf("%d",&m);
}
n不等于1时候就循环了 估计得有问题
[解决办法]
- C/C++ code
#include<stdio.h>void get_choice();int fun (int,int);int main(void){ int a,b,m; printf("Please input two number:\n"); scanf("%d%d",&a,&b); m=fun(a,b); printf("the number you input is %d and it is ok!\n",m); return 0;}void get_choice(){ printf("Please choose one of the following\n"); printf("1) copy files 2) move files\n"); printf("3) remove files 4) quit\n"); printf("Enter the number of your choice\n");}int fun(int min,int max){ printf("%d %d\n",min,max); int m=0,n=0; getchar(); //用于清除上次scanf()输入时缓冲区里残留的换行符 n=scanf("%d",&m); while(n!=1||m<min||m>max) //这里该是m { get_choice(); n=scanf("%d",&m); } return m;}