4

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?

5 Answers 5

10

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
Sign up to request clarification or add additional context in comments.

2 Comments

+1, it is important to note that && is a sequence point (old C++03 parlance) and thus it is guaranteed that (i<0.0) will only be evaluated after cin >> i.
This was it; I overlooked that fact that while does a bool check. Makes total sense.
4

The expression (cin >> i) does not return a double.

You can write the same statement, without a break, as:

double i;
while ((cin >> i) && (i < 0.0)) {
    cout << "ENTER A POSITIVE NUMBER: "; 
}

3 Comments

Reverting the -1 as the code is correct. You might want to revisit the explanation, as cin >> i does not yield a boolean value, but a reference to the stream (which can be then converted to a bool)
This is all good, simple practice for you guys. It was all about my oversight regarding the while and boolean.
4

you want to check the value of i, not the return of cin

while((cin >> i) && ( i < 0.0))
{
    cout << "ENTER A POSITIVE NUMBER: ";
}

Comments

2

The return value of cin >> i is the stream, not the read value. This is to allow chaining of operands

cin >> i >> j;

You could try this:

while( (cin >> i, i) < 0. )
{
    cout << "ENTER A POSITIVE NUMBER: ";
}

The comma operator should return the value of i but I haven't tested it.

EDIT: Don't use this approach, as David Rodríguez has noted this discards the result of the read. Use while( (cin >>i) && (i<0.) ) instead.

2 Comments

-1 The problem with this approach is that it completely ignores the result of the read. Say that i is negative and the user closes the input stream (or types a letter which fails to parse as a double and sets the fail bit in the stream), then the subsequent reads will fail, and the value of i will not be updated being still positive in an infinite loop.
Yes, I used the while((cin >> i) && (i < 0))
0

Do:

while( cin >> i && i < 0.0 )
{
    cout << "ENTER A POSITIVE NUMBER: ";
}

The error is caused due to the fact that the expression (cin >> i) is not valid for comparison with a double.

3 Comments

It's been a while since I've played with operator,; are you guaranteed that cin >> i is run before the i < 0.0 when the thing on the left of the comma is an object?
Same issue as with Alex. The comma operator discards the first expression. If the input is -1 a you run into an infinite loop. In the first iteration i is set to -1, after that the read fails, i is unmodified and you enter an infinite loop.
@MaxLybbert: Yes, that is guaranteed by the standard, but this answer has other issues.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.