求助:关于这个程序的循环问题..
#include<stdio.h>#include<stdlib.h>
int get_num (void);
char get_first (void);
char get_choice (void);
int add (void);
int sub (void);
int mul (void);
int div (void);
int main (void)
{
int choice;
printf("HELLO!!!\n");
while ((choice=get_choice())!='q')
{
switch (choice)
{
case 'a':add();
break;
case 's':sub();
break;
case 'm':mul();
break;
case 'd':div();
break;
default:printf("input error");
}
}
printf("bye");
system("pause");
}
char get_choice ()
{
char ch;
printf("Enter the operation of your chice\n");
printf("a.add s.subtrct\n");
printf("m.multiply d.divide\n");
printf("q.quit\n");
ch=get_first();
while (ch!='a'&&ch!='s'&&ch!='m'&&ch!='d'&&ch!='q')
{
printf("please input a,s,m,d or q\n");
ch=get_first();
}
return ch;
}
char get_first ()
{
char ch;
ch=getchar();
while(getchar()!='\n')
continue;
return ch;
}
int add ()
{
int num_1,num_2;
printf("Enter the first number:\n");
num_1=get_num();
printf("Enter the second number\n");
num_2=get_num();
printf("%d+%d=%d",num_1,num_2,num_1+num_2);
}
int sub ()
{
int num_1,num_2;
printf("Enter the first number:\n");
num_1=get_num();
printf("Enter the second number\n");
num_2=get_num();
printf("%dX%d=%d",num_1,num_2,num_1*num_2);
}
int mul ()
{
int num_1,num_2;
printf("Enter the first number:\n");
num_1=get_num();
printf("Enter the second number\n");
num_2=get_num();
printf("%d-%d=%d",num_1,num_2,num_1-num_2);
}
int div ()
{
int num_1,num_2;
printf("Enter the first number:\n");
num_1=get_num();
printf("Enter the second number\n");
num_2=get_num();
printf("%d/%d=%d",num_1,num_2,num_1/num_2);
}
int get_num()
{
int num;
char ch;
while (scanf("%d",&num)!=1)
{
while ((ch=getchar())!='\n')
putchar(ch);
printf("is not a integer,please input such as 1, 85,65");
}
return num;
}
运行后正常,但进入第一次循环,不管输入什么都显示please input a,s,m,d or q,是哪里弄错了啊,虚心求教。。。