lundi 18 janvier 2021

(C++) error: cannot resolve overloaded function 'operator==' based on conversion to type 'bool'

I was trying to make a calculator in which we have to input two numbers and then to give an operator, choose from 100, 200, 300, 400 in which 100 is for addition and 200 for subtraction and 300 for multiplication and 400 for division. The code is the following:

#include<iostream>
using namespace std;

int main(){
    int number1;
    int number2;
    cout<<"Input two numbers";
    cin>>number1>>number2;

     cout<<"Input 100 for addition, 200 for subtraction, 300 for multiplication and 400 for division";

    int operator;
    cin>>operator;

    if(operator==100){
        int sum=number1+number2;
        cout<<sum;
    }

    else if(operator==200){
        int difference=number1-number2;
        cout<<difference;
    }

    else if(operator==300){
        int product=number1*number2;
        cout<<product;
    }

    else if(operator==400){
        int quotient=number1/number2;
        cout<<quotient;
    }

    else{
        cout<<"Wrong Input. Try Again.";
    }

}

When I run this, no matter how much I improve this code and find errors, it still gives me the error:

 In function 'int main()':
calculator-copy.cpp:15:21: error: cannot resolve overloaded function 'operator==' based on conversion to type 'bool'
     if(operator==100){
                     ^
calculator-copy.cpp:20:26: error: cannot resolve overloaded function 'operator==' based on conversion to type 'bool'
     else if(operator==200){
                          ^
calculator-copy.cpp:25:26: error: cannot resolve overloaded function 'operator==' based on conversion to type 'bool'
     else if(operator==300){
                          ^
calculator-copy.cpp:30:26: error: cannot resolve overloaded function 'operator==' based on conversion to type 'bool'
     else if(operator==400){
                          ^

Can anyone help me in this.

Aucun commentaire:

Enregistrer un commentaire