0

我试图让 scanf 以数字的形式接收用户的输入,然后使用我要求的两个数字来执行一些简单的数学运算,我遇到的问题是,无论我输入什么数字,他们总是只是吐出 1 任何帮助将不胜感激

包括

 int main(int argc, char const *argv[])
{
float in1,in2,value;
char c;

printf("Enter the operation of your choice:\n");
printf("A. add           S. subtract\n");
printf("M. multiply      D. divide\n");
printf("Q. quit\n");
c=getchar();

while( c !='Q' && c!='q')
{
    printf("please enter your first number:");
    scanf("%g", &in1);
    printf("%g\n",in1 );

    printf("please enter your second number:");
    scanf("%g", &in2);
    printf("%g\n",in2 );

    if(c == 'S' || c =='s')
        if(in2 == 0)
        {
            printf("you have selected subtration however\n");
            printf("the value you entered was 0\n");
            printf("please enter a value greater than 0\n");
            scanf("%lg",&in2);
        }

    if(c == 'a' || c == 'A')
    {
        value = in1+in2;
        printf("%lg + %lg = %lg\n",in1,in2,value );
    }

    if(c == 's' || c == 'S')
    {
        value = in1-in2;
        printf("%lg - %lg = %lg\n",in1,in2,value );
    }   

    if(c == 'm' || c == 'M')
    {
        value = in1*in2;
        printf("%lg * %lg = %lg\n",in1,in2,value );
    }   

    if(c == 'd' || c == 'D')
    {
        value = in1/in2;
        printf("%lg / %lg = %lg\n",in1,in2,value );
    }   
    if (c !='Q' && c !='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");    
    }
    c=getchar();
}
return 0;
}
4

1 回答 1

0

您能否将您的变量定义为double而不是float?另外,我觉得您getchar()在 while 循环结束时还需要一个。当您第一次输入第二个数字时,回车键被视为c = getchar(); 要再次阅读该选项,您必须包含另一个c = getchar().

于 2013-02-13T16:02:33.900 回答