为了账号安全,请及时绑定邮箱和手机立即绑定

不知是什么原因,我在编译某些代码时总是会遇到奇怪的情况,希望大家能看一下, 谢谢!

不知是什么原因,我在编译某些代码时总是会遇到奇怪的情况,希望大家能看一下, 谢谢!

C C++
AIxer 2016-02-01 12:00:09
// Program 3.11 A calculator#include<stdio.h>int main(){    /*创建并输入算式*/    double number1 = 0.0;    //first operand value a decimal number    double number2 = 0.0;    //Second operand value a decimal number    char operation = 0;      //Operation - must be +-,*./,or %        start:    printf("\nEnter the calculation\n");    scanf("%lf %c %lf",&number1,&operation,&number2);        /*检查输入是否正确*/    switch(operation)    {        case '+':                                                                     //No checks necessary for add            printf("= %lf\n",number1 + number2);            break;        case '-':                                                                     //No checks necessary for subtract            printf("= %lf\n",number1 - number2);            break;        case '*':                                                                     //No checks necessary for multiply            printf("= %lf\n",number1 * number2);            break;        case '/':                                                                     //Check second oprand for zero            if(number2 == 0)                printf("\n\n\aDivision by zero error!\n");            else                printf("= %lf\n",number1 / number2);            break;        case '%':                                                                //Check second oprand for zero             if((long)number2 == 0)                printf("\n\n\aDivision by zero error!\n");            else                printf("= %lf\n",(long)number1 % (long)number2);            break;        default :                                                                //Operation is invalid if we get to here            printf("\n\n\a Illegal operation");            break;    }  char answer ='0';  printf("\n Do you want to do another calculation? (y or n):\n");  scanf("  %c", &answer);/************************************************  写成这样:scanf("%c",&answer);  在控制字符串前删一个空格为什么就不行呢? ********************************************* */if(answer == 'y'  || answer == 'Y')    goto start ;    return 0;}
查看完整描述

2 回答

已采纳
?
MadMarical

TA贡献79条经验 获得超122个赞

你好。在c语言中空格和回车都属于结束标志符,如果你完成了输入按下空格或者回车都属于告诉计算机接收你的输入。如果像将空格当作字符一般删除,scanf是达不到目的的。在c语言中使用gets方法输入可以获取到空格字符,但是由于gets的安全性问题,不提倡使用。

不知道楼主是否问的这个问题。

查看完整回答
反对 回复 2016-02-02
  • 2 回答
  • 1 关注
  • 1608 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信