首页 > 用户发贴区 > 编程问题提问区 > 这个程序那里出错呢
2008
10-18

这个程序那里出错呢


#include <stdio.h>
#include <dos.h>
main()
{
      float num1,num2,result;
      char operator;
      char answer=’Y';
      int right;
      printf(“**********************欢迎进入四则运算系统************************\n”);
      do
      {
          printf(“请输入num1,operator,num2\n”);
          scanf(“%f%c%f”,&num1,&operator,&num2);
          printf(“请输入你的答案\n”);
          scanf(“%f”,&result);
          switch(operator)
          {
              case ‘+’:if(result==(num1+num2)) right=1;
                       else right=0;
                       break;
             
              case ‘-’:if(result==(num1-num2)) right=1;
                       else right=0;
                       break;
             
              case ‘*’:if(result==(num1*num2)) right=1;
                       else right=0;
                       break;
             
              case ‘/’:if(num2==0) {printf(“输入有误”);right=0;break;}
                       else
                       {
                           if(result==(num1/num2)) right=1;
                           else right=0;
                           break;
                       }
              default:printf(“输入有误\n”);
          }
          if(right)
             printf(“答案正确\n”);
          else
             printf(“答案错误\n”);
         
             printf(“是否退出系统?(Y or N)\n”);  /*提示用户是否继续运算*/
             scanf(“%c”,&answer);
      }
      while((answer==’Y')||(answer==’y'));
      system(“pause”);

              
这个程序在最后的是不会执行do-while的的循环,而是直接结束程序


这个程序那里出错呢》有 5 条评论

  1. Perfect 说:

    在scanf(“%c”,&answer);前加上一句getchar()

  2. bearangel 说:

    为什么要加上

    getchar()  呢????

  3. Perfect 说:

    用来接收“回车”字符

  4. Perfect 说:

    例如

    你输入  1+2之后肯定要敲“回车”才能运行

    getchar()就是用来接收这个回车

  5. wyong 说:

    #include <stdio.h>
    #include <dos.h>
    main()
    {
          float num1,num2,result;
          char operator;
          char answer;
          int right;
          printf(“**********************welcome************************\n”);
          do
          {
          printf(“please input num1,operator,num2\n”);
              scanf(“%f%c%f”,&num1,&operator,&num2);
          printf(“your answer is \n”);
              scanf(“%f”,&result);
              switch(operator)
              {
                  case ‘+’:if(result==(num1+num2)) right=1;
                           else right=0;
                           break;

                  case ‘-’:if(result==(num1-num2)) right=1;
                           else right=0;
                           break;

                  case ‘*’:if(result==(num1*num2)) right=1;
                           else right=0;
                           break;

              case ‘/’:if(num2==0) {printf(“scanf error”);right=0;break;}
                           else
                           {
                               if(result==(num1/num2)) right=1;
                               else right=0;
                               break;
                           }
              default:printf(“scanf error\n”);
              }
              if(right)
             printf(“right ans\n”);
              else
             printf(“wrong ans\n”);
             printf(“dou you want to exit ?(Y or N)\n”);
             getchar();
             scanf(“%c”,&answer);
          }
          while((answer==’N')||(answer==’n'));
    }

留下一个回复