Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

negative value input in while loop c++

Tags:

c++

while-loop

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?

like image 792
Snerd Avatar asked Dec 01 '22 05:12

Snerd


1 Answers

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
like image 102
Coding Mash Avatar answered Jan 16 '23 03:01

Coding Mash