【问题标题】:I keep getting this error: invalid operands to binary ^ (have 'double' and 'double')我不断收到此错误:二进制 ^ 的无效操作数(具有“双”和“双”)
【发布时间】:2013-01-24 17:44:26
【问题描述】:

每当我运行代码时,我的第 52 行和第 61 行都会不断给我相同的错误消息。

#include<stdio.h>
#include<math.h>

double getinput(void);
double calcwindchillold(double v, double t);
double calcwindchillnew(double v, double t);
void printResults(double windchillold, double windchillnew);

int main(void)
{
    double v = 0;
    double t = 0;
    double windchillold = 0;
    double windchillnew = 0;

    v = getinput();
    t = getinput();
    windchillold = calcwindchillold(v,t);
    windchillnew = calcwindchillnew(v,t);
    return 0;
}
double getinput(void)
{
    double v,t;
    printf("Input the wind speed in MPH: ");
    scanf("%lf", &v);
    printf("Input the temperature in degrees Fahrenheit: ");
    scanf("%lf", &t);


    return v,t;

}

double calcwindchillold(double v, double t)
{
    double windchillold;

    windchillold = ((0.3 * v^0.5) + 0.474 - (0.02 * v) * (t - 91.4) + 91.4);
// in the line above this is the error.

    return windchillold;
}

double calcwindchillnew(double v, double t)
{
    double windchillnew;

    windchillnew = 35.74 + 0.6215*t - 35.75*(v^0.16) + 0.4275 * t * v^0.16;
// in the line above this is the second error.

    return windchillnew;
}

void printResults(double windchillold, double windchillnew)
{
    printf("The wind chill using the old formula is: %lf F\n", windchillold);
    printf("The wind chill using the new formula is: %lf F\n", windchillnew);
}

这让调试系统说: 错误:二进制 ^ 的操作数无效(有 'double' 和 'double')

查看了其他也出现“双重”错误的脚本,但无法使用该信息来帮助自己。

我知道这可能是我看过的一些简单的事情。

【问题讨论】:

标签: c binary double operands


【解决方案1】:

在 C 中,^ 是异或运算符 (XOR),而不是幂运算符。您不能对两个浮点值进行异或运算。

要求幂,您可以使用math.h 中的pow(3) 函数。

【讨论】:

    【解决方案2】:

    位运算符不适用于浮点类型的操作数。操作数必须是整数类型。

    (C99, 6.5.11p2 按位异或运算符) "每个操作数都应为整数类型。"

    ^ 是 C 中的按位异或运算符。

    要使用电源操作,请使用math.h 中声明的powpowf 函数。

    【讨论】:

      【解决方案3】:
      #include <math.h>
      double pow( double base, double exp );
      

      在 C 中你不能返回多个值,所以请像这样执行单个函数...

      double getinput(const char* message) {
          double retval;
          printf("%s: ", message);
          scanf("%lf", &retval);
          return retval;
      }
      

      在了解指针以及如何掌握操作系统之前,请尽量让代码尽可能简单。

      希望对你有帮助:)

      【讨论】:

        【解决方案4】:

        另一个问题:

        return v,t;
        

        C 中不能有多个返回值。

        您可以在调用时将其作为输出参数执行,也可以创建单独的函数。 例如:

        void getinput(double* v_out, double* t_out)
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2021-04-16
          • 1970-01-01
          • 1970-01-01
          • 2018-10-25
          • 2017-07-06
          • 2016-01-06
          • 1970-01-01
          相关资源
          最近更新 更多