有人可以解释这个C错误消息吗?

时间:2015-05-06 17:06:50

标签: c xcode switch-statement

我正在编写一个程序来获取两个数字和一个运算符并执行计算。我之前已经在Objective-C中编写过它,并且我正在回过头来尝试用C语句用switch语句编写它。我一直收到以下错误消息: “将'double'传递给不兼容类型'cont char *''的参数,与我调用并打印我的函数的行相关(在return 0;之前)。我想了解究竟是什么导致了这个问题(而不仅仅是找出如何修复它)。我想确保无论出于何种原因我都可以避免这种情况发生。

 #include <stdio.h>
    double calculator(double x,char y, double z) {
        switch (y) {
            case '+':
                return x+z;
                break;
             case '-':
                return x-z;
                break;
            case '/':
                return x/z;
                break;
            case '*':
                return x*z;
                break;
            default:
                return 0;
                break;
                }
    }


    int main(int argc, const char * argv[]) {

        char y;
        double x, z;

        printf ("Please enter a number to be operated on\n");
        scanf("%lf", &x);
        printf ("Enter a valid operator\n");
        scanf("%s", &y);
        printf("Please enter a second number to complete the calculation\n");
        scanf("%lf", &x);

        printf (calculator(x,y,z));

                return 0;

    }

3 个答案:

答案 0 :(得分:2)

printf()的原型是

printf(const char*,...);

但是你将double传递给它,因此错误

答案 1 :(得分:1)

你的程序有几个问题,每一行都可能出错

  1. 您使用char说明符将scanf()传递给"%s"scanf()会认为它是char指针,它会尝试在其中至少存储2个字符,但只有一个空格。正确的说明符是"%c",您应该在c前面留一个空格,否则它会消耗前一个'\n'缓冲区中的scanf()

  2. 当您致电scanf()时,您不会检查calculator()的返回值,这会导致未定义的行为。

  3. 您将double作为第一个参数传递给printf(),它需要一个char指针,最好指向一个字符串文字,以打印double你需要

    printf("%f", doubleValue);
    
  4. 此代码将修复我上面提到的所有内容

    #include <stdio.h>
    
    double calculator(double x, char operator double y)
    {
        switch (operator)
        {
        case '+':
            return x + y;
         case '-':
            return x - y;
        case '/':
            if (y == 0)
                return 0.0; /* division by zero */
            return x / y;
        case '*':
            return x * y;
        default:
            break;
        }
        return 0.0;
    }
    
    
    int main(int argc, const char * argv[])
    {
        char   operator;
        double x, y;
    
        printf("Please enter a number to be operated on\n");
        if (scanf("%lf", &x) != 1)
         {
            fprintf(stderr, "Invalid input\n");
            return -1;
         }
    
        printf ("Enter a valid operator\n");
        if (scanf(" %c", &operator) != 1)
         {
            fprintf(stderr, "Invalid input\n");
            return -1;
         }
    
        printf("Please enter a second number to complete the calculation\n");
        if (scanf("%lf", &y) != 1)
         {
            fprintf(stderr, "Invalid input\n");
            return -1;
         }
        printf("%f %c %f = %f\n", x, operator, y, calculator(x, operator, y));
    
        return 0;
    }
    

答案 2 :(得分:0)

printf函数的原型是:

int printf( const char *format ,…)

format - 它是常量字符串。

 double calculator(double x,char y, double z) {

但函数返回double值。 所以它打印错误信息为

  

“将'double'传递给不兼容类型'cont char *'”

的参数
相关问题