2015-09-06 209 views
2

我是一個初學者編程,我目前正在嘗試做一個轉換程序,從千克到磅,反之亦然。我不擅長閱讀錯誤代碼,所以有人可以告訴我,我做錯了什麼。C++錯誤:無效操作數的類型'雙'和<未解析的重載函數類型'二進制'操作員'

#include <iostream> 

using namespace std; 

int main() 
{ 

    char response; 

    double Kilo_const = 0.45, Pound_const = 2.20, Kilo_output, Pound_output; 

    double Kilo_input, Pound_input; 

    cout << "Choose the input unit \nk = kilo and p = pound" << endl; 

    cin >> response; 

    if (response == 'k'){ 
     cin >> Kilo_input; 
     cout << Pound_output = Kilo_input * Pound_const << endl; 
    } 
    else (response == 'p'){ 
     cin >> Pound_input; 
     cout << Kilo_output = Pound_input * Kilo_const << endl; 
    } 
    return 0; 
} 
+0

請張貼錯誤消息 – Nidhoegger

回答

1

您的問題是第21行:

cout << Pound_output = Kilo_input * Pound_const << endl; 

你正在嘗試做的,是一個值賦給Pound_output,然後把它傳遞給cout,這不會工作。

你既可以做這種方式(注意paranthesis THX阿倫!):

cout << (Pound_output = Kilo_input * Pound_const) << endl; 

Pound_output = Kilo_input * Pound_const; 
cout << Pound_output << endl; 

將首先進行轉換,並打印出來,或者你可以做

cout << Kilo_input * Pound_const << endl; 

它不會使用變量來存儲結果,而是立即打印它。

同樣適用於您的第二次轉換。

此外,您的if語句中還有一點小錯誤。語法是

if (...) { } else if (...) { } 

你忘了第二個if。如果不存在,else標籤就沒有條件,並且只要第一條語句失敗就會執行。注意區別:

if (a == 1) { cout << "Execute on a = 1"; } else { cout << "Execute on a != 1"; } 

if (a == 1) { cout << "Execute on a = 1"; } else if (a == 2) { cout << "Execute on a != 1 and a = 2"; } 
+1

原將與右邊括號裏的工作,不是嗎?賦值只是一個表達式,爲什麼它不能寫入'cout'? –

+0

你是絕對正確的!編輯答案 – Nidhoegger