首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

再次输入时出有关问题

2012-03-29 
求助再次输入时出问题#includestdio.hchar memu(void)float shuzi(void)void add(float a,float b)vo

求助再次输入时出问题
#include<stdio.h>
char memu(void);
float shuzi(void);
void add(float a,float b);
void s(float a,float b);
void m(float a,float b);
void d(float a,float b);
int main(void)
{
  float a,b;
  char ch;

  while((ch=memu())!='q')
  {
  printf("Enter first number:");
  a=shuzi();
  printf("Enter second number:");
  b=shuzi();
  if(ch=='a')
  add(a,b);
  if(ch=='s')
  s(a,b);
  if(ch=='m')
  m(a,b);
  if(ch=='d')
  d(a,b);
  printf("%c",ch);
  }
  printf("bye\n");
  return 0;
}
char memu(void)
{
  char ch;
  ch='l';

  while(ch!='a'||ch!='s'||ch!='m'||ch!='d'||ch!='q')
  {
  printf("Enter the operation of your choice:\n");
  printf("a.add s.subtract\n");
  printf("m.multiply d.divide\n");
  printf("q.quit\n");
  ch=getchar();
  while(getchar()!='\n')
  continue;
  switch(ch)
  {
  case 'a':break;
  case 's':break;
  case 'm':break;
  case 'd':break;
  case 'q':break;
  default:printf("what?%c\n",ch);break;
  }
  return ch;
  }
}
float shuzi(void)
{
  float q;
  char ch;

  while((scanf("%f",&q))!=1)
  {
  while((ch=getchar())!='\n')
  putchar(ch);
  printf(" is not an number\n");
  printf("Please enter a number,such as 2.5,-1.78E8,or3:");
  }
  return q;
}
void add(float a,float b)
{
  printf("%f+%f=%f\n",a,b,a+b);
}
void s(float a,float b)
{
  printf("%f+%f=%f\n",a,b,a-b);
}
void m(float a,float b)
{
  printf("%f*%f=%f\n",a,b,a*b);
}
void d(float a,float b)
{
  printf("%f/%f=%f",a,b,a/b);
}


[解决办法]
while((scanf("%f",&q))!=1)

//应该是这个的问题吧 不能这样写吧

scanf提取到了键盘缓冲区的
[解决办法]
在scanf前面加rewind(stdin);
[解决办法]

C/C++ code
char memu(void){  char ch;  ch='l';  while(ch!='a'||ch!='s'||ch!='m'||ch!='d'||ch!='q')  {  printf("Enter the operation of your choice:\n");  printf("a.add s.subtract\n");  printf("m.multiply d.divide\n");  printf("q.quit\n");  ch=getchar();  if(ch=='\n')ch=getchar();//加上这行  printf("get:%c\n",ch);  while(getchar()!='\n')  continue;  switch(ch)  {  case 'a':break;  case 's':break;  case 'm':break;  case 'd':break;  case 'q':break;  default:printf("what?%c\n",ch);break;  }  return ch;  }  } 

热点排行