1
votes

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;
}
1
cout << average = (num1 + num2) / 2; probably doesn't do what you want. Check the operator precedence.nwp
cin >> num1, num2; also doesn't do what you think it doesNathanOliver
average = (num1 + num2) / 2; cout << average;Gillespie
What's the cout << doing there? You're displaying the average later.molbdnilo
Maxim's answer is correct also change "endl" to endlPriyansh

1 Answers

3
votes

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;