I have this code:
double i;
while(cin >> i)
{
if ( i < 0.0)
cout << "ENTER A POSITIVE NUMBER: ";
else
break;
}
I want code like this ( I don't want to use break):
while((cin >> i) < 0.0)
{
cout << "ENTER A POSITIVE NUMBER: ";
}
I get an error on this line: while((cin >> i) < 0.0)
saying invalid operands to binary expression
.
What am I missing?
Use it like this.
while ((cin >> i) && (i < 0.0))
The overloaded function for cin
returns an object by reference of istream class
. So you cannot compare it to a double value.
cin >> i
|-------| //this is an object of istream class
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With