I’ve been trying to get the arithmetic if operator to work but I just can’t seem to do it.
I’m new to C++ and still learning the basics but I’m just wondering if I’m using this operator correctly. It’s supposed to return false if x < y. Is this the correct way to do it? I’m aware I can use an if else but I’m just wondering if I can also do it like this and if I can what I’m doing wrong.
#include <iostream>
using namespace std;
int x =0;
int y =1;
bool test()
{
return (x < y) ? true : false;
}
int main()
{
cout << test;
return 0;
}
You state:
And you’re trying to learn about the arithmetic if (ternary) operator, so ignore all the advice to eliminate it.
The first part after the
?is what will be returned if the expression is true, and the second part after the:is what will be returned if it is not true. Thus you have your return values reversed, and it should be: