标题:求助:关于这个程序的循环问题..
只看楼主
z562586756
Rank: 1
等 级:新手上路
帖 子:18
专家分:1
注 册:2011-4-30
结帖率:16.67%
 问题点数:0 回复次数:5 
求助:关于这个程序的循环问题..
#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,是哪里弄错了啊,虚心求教。。。
                       
                       
                  
                  
               
         
                     
         

   
搜索更多相关主题的帖子: default 
2011-07-29 10:26
laoyang103
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:内蒙古包头
等 级:贵宾
威 望:19
帖 子:3082
专家分:11056
注 册:2010-5-22
得分:0 
你的代码貌似不全  能不能把全的贴出来  这样我也好帮你调试

                                         
===========深入<----------------->浅出============
2011-07-29 10:40
吴辉
Rank: 3Rank: 3
来 自:湖南
等 级:论坛游侠
帖 子:52
专家分:199
注 册:2011-3-27
得分:0 
小小修改一下就行了
#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");
    choice=get_choice();                       //加一句
    //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");
          return 0;
         
}
 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\n",num_1,num_2,num_1+num_2);
                  return 0;
               }
                  
 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\n",num_1,num_2,num_1*num_2);
                   return 0;
                   }
                  
 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\n",num_1,num_2,num_1-num_2);
               return 0;
                   }
                  
 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\n",num_1,num_2,num_1/num_2);
 return 0;      
 }
                  
 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;
   }



[ 本帖最后由 吴辉 于 2011-7-29 11:36 编辑 ]
2011-07-29 11:35
laoyang103
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:内蒙古包头
等 级:贵宾
威 望:19
帖 子:3082
专家分:11056
注 册:2010-5-22
得分:0 
看错了  原来是底下没有缩进  抱歉 楼上的帮你解决了

                                         
===========深入<----------------->浅出============
2011-07-29 11:37
z562586756
Rank: 1
等 级:新手上路
帖 子:18
专家分:1
注 册:2011-4-30
得分:0 
回复 3楼 吴辉
这样子好像还是不可以的啊。。
2011-07-29 12:09
吴辉
Rank: 3Rank: 3
来 自:湖南
等 级:论坛游侠
帖 子:52
专家分:199
注 册:2011-3-27
得分:0 
int add (void);
int sub (void);
int mul (void);
int div (void);
int main (void)
这几个函数里面加return 0;试试。。。
2011-07-29 14:32



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-346335-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.282580 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved