簡體   English   中英

我不斷收到此錯誤:二進制^的無效操​​作數(具有“ double”和“ double”)

[英]I keep getting this error: invalid operands to binary ^ (have 'double' and 'double')

每當我運行代碼時,第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 ^是異或運算符(XOR),而不是一個冪運算符。 您不能對兩個浮點值進行XOR。

要求冪,可以使用math.hpow(3)函數。

按位運算符不適用於浮點類型的操作數。 操作數必須具有整數類型。

(C99,6.5.11p2按位異或運算符)“每個操作數都應具有整數類型。”

^是C中的按位異或運算符。

要使用冪運算,請使用math.h聲明的powpowf函數。

#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;
}

在學習指針以及如何掌握操作系統之前,請嘗試使代碼盡可能簡單。

希望能幫助到你 :)

另一個問題:

return v,t;

C中不能有多個返回值。

您可以將其作為調用中的out參數來執行,也可以創建單獨的函數。 例如:

void getinput(double* v_out, double* t_out)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM