简体   繁体   中英

C++ error: invalid Operands of types 'double' and <unresolved overloaded function type' to binary 'operator'

Im a beginner to programming and I am currently trying to make a conversion program from kg to pounds and vice versa. Im not good at reading the error codes, so can somebody please tell me, what I've done wrong.

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

You problem is Line 21:

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

what you are trying to do here is assign a value to Pound_output and then pass it to cout , which wont work.

You could either do it this way (note the paranthesis! Thx to Alan):

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

or

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

which will first do the conversion and the print it out, or you can do

cout << Kilo_input * Pound_const << endl;

which will not use a variable to store the result, but print it immediately instead.

Same applies to your second conversion.

Also you have a little second mistake with your if clause. The syntax is

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

where you forgot the second if . If that is not present, the else tag has no conditions and will execute whenever the first statement fails. Note the difference:

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

and

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM