No operator ">=" matches these operands error in c++ when using while loop

RealSlimShady

Here is the code

int main()
{
    int x;
    while((cin >> x) >= 2){
      cout<<"Greater than 2";
    }
}

and I am using these preprocessor directives

#include <iostream>
#include <ios>
#include <string>
#include <iomanip>

using std::cout;  using std::cin;

I am getting error in the while loop, it says this:

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

I am new to c++

HARSH MITTAL

In the while condition, you are comparing cin>>x and 2.

I believe you wanted to check for conditions where x>= 2. But, in your code, the compiler is comparing the output of cin>>x with 2. As the output of the cin operator is not INT, hence you are getting the error.

For more information on this, you can refer to: https://devdocs.io/cpp/io/cin

As far as correcting the code is concerned, try while( cin>>x && x>=2) because here the compiler will first execute the cin and then it will execute the comparision.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

error: no operator "<" matches these operands

Error: no operator "==" matches these operands

no operator ""<<"" matches these operands error

Error : no operator " != " matches these operands

no operator "=" matches these operands error

C++ - Error - No operator "[]" matches these operands

C++ No operator [] matches these operands

No operator "<<" matches these operands - C++

No operator "[ ]" matches these operands C++

no operator "<<" matches these operands recursive tower of hanoi error

Error during implementation of operator>> :C++ no operator matches these operands operand types are: std::istream >> const double error

CS50 readability error: expected expression in C when using ! operator on a while loop

IntelliSense: no operator "<<" matches these operands

no operator "/" matches these operands

"no operator >> matches these operands"

Error C++: no operator matches these operands. operand types are: std::ostream << void

No operator "<<" matches these operands error between an object and a string literal

C++ - no operator "<<" matches these operands directory_iterator()

C++) E0349 no operator matches these operands occured

Operator '&&' cannot be applied to operands of type 'bool' and 'string' when using if else statement in C# for unity

Error when using while loop in another while loop

what to do when got error operator " required two operands?

E0349 no operator ">>" matches these operands

No Operator = Matches Operands - DX11

more than one operator "[]" matches these operands

E0349 no operator "<<" matches these operands

operator & cannot be applied to operands of type int and bool when using bitwise and

Error when using overloaded operator () C++

C# ERROR Operator * cannot be applied to operands of type 'string' and 'string'