Terrence Long
Terrence Long

Reputation: 13

no operator "=" matches these operands operand types are std::basic_ostream<char, std::char_traits<char>> = int

I'm getting the error no operator "=" matches these operands operand types are std::basic_ostream> = int when running code in C++ and i'm not sure whats actually causing the error.

#include <iostream>
#include <string>
#include <fstream>
#include <cctype>
using namespace std;

int main()
{
    int num1, num2;
    double average;

    // Input 2 integers
    cout << "Enter two integers separated by one or more spaces: ";
    cin >> num1, num2;

    //Find and display their average
    cout << average = (num1 + num2) / 2;

    cout << "\nThe average of these 2 numbers is " << average << "endl";

    return 0;
}

Upvotes: 1

Views: 15105

Answers (1)

Maxim Egorushkin
Maxim Egorushkin

Reputation: 136505

The compiler treats

cout << average = (num1 + num2) / 2;

As:

(cout << average) = ((num1 + num2) / 2);

See C++ operator precedence for more details.

Fix:

cout << (average = (num1 + num2) / 2);

Prefer simpler statements:

average = (num1 + num2) / 2;
cout << average;

Also

cin >> num1, num2;

Should be

cin >> num1 >> num2;

Upvotes: 3

Related Questions