2013-11-01 101 views
-4
#include <iostream> 
#include <cmath> 
using namespace std; 
double T; 
double V; 
double WC(double T, double V); 
void index(double WC(double T, double V)); 

    int main(void) 
{ 
    cout<<"Please enter your T followed by your V"<<endl; 
    cin>>T>>V; 
    cout<<index; 
} 
double WC(double, double) 
{ 
    if(V>4.8) 
     return 13.12+0.6215*T-11.37*pow(V,0.16)+0.3965*T*pow(V,0.16); 
    else 
     return T; 
} 
void index(double WC(double,double)) 
{ 
    if (WC(T,V)<=0&&WC(T,V)>=-25) 
    { 
     cout<<"Discomfort"; 
    } 
    if (WC(T,V)<-25&&WC(T,V)>=-45) 
    { 
     cout<<"Risk of skin freezing (frostbite)"; 
    } 
    if (WC(T,V)<-45&&WC(T,V)>=-60) 
    { 
     cout<<"Exposed skin may freeze within minutes"; 
    } 
    if (WC(T,V)<-60) 
    { 
     cout<<"Exposed skin may freeze in under 2 minutes"; 
    } 
} 

我不明白爲什麼這會輸出像「010F11B8」這樣的隨機亂碼,它只能根據輸入的溫度和風速輸出。我不明白爲什麼這個簡單的程序輸出亂碼?

+1

你是不是叫你'index'功能並打印出索引函數的地址 –

+0

假設你在UNIX/Linux中,這可能會有幫助:http://sourceware.org/gdb/current/onlinedocs/gdb/ –

+0

爲了詳細說明'Ed S'' s點:什麼是'T'?什麼是'V'?什麼是'WC'?您只使用一個提示並同時接受輸入而不是即時輸入提示輸入?等... – nhgrif

回答

1

我想你要找的東西是這樣的:

working example

打電話給你需要這樣做的指數函數:

#include <iostream> 
#include <cmath> 
using namespace std; 
double T; 
double V; 
double WC(double T, double V); 
void index(double WC(double T, double V)); 

int main(void) 
{ 

    V = 5.0; 
    T = -60.0; 
    // declare a function pointer which accepts two doubles and returns a double 
    double (*wcPtr)(double, double); 

    // initialise function pointer 
    wcPtr = &WC; 

    cout << "Please enter your T followed by your V" << endl; 

    // call function pointer 
    index(wcPtr); 
} 
double WC(double T, double V) 
{ 
    if(V>4.8) 
     return 13.12+0.6215*T-11.37*pow(V,0.16)+0.3965*T*pow(V,0.16); 
    else 
     return T; 
} 
void index(double WC(double T,double V)) 
{ 
    if (WC(T,V)<=0&&WC(T,V)>=-25) 
    { 
     cout<<"Discomfort"; 
    } 
    if (WC(T,V)<-25&&WC(T,V)>=-45) 
    { 
     cout<<"Risk of skin freezing (frostbite)"; 
    } 
    if (WC(T,V)<-45&&WC(T,V)>=-60) 
    { 
     cout<<"Exposed skin may freeze within minutes"; 
    } 
    if (WC(T,V)<-60) 
    { 
     cout<<"Exposed skin may freeze in under 2 minutes"; 
    } 
} 
2

你不打電話index,你打印的地址。方法調用需要parens index()

+0

幾乎與我的相同的措辭... BTW,'索引'採取論據 –

+1

@BryanChen:我知道它的確如此。你應該發佈了一個答案,而不是評論。 –

+0

我試過cout << index(WC);但是這不起作用,因爲「沒有操作符匹配這些操作數」? – user2923961

相關問題