Taking input as condition into while loop

In C I can write the following code. There is no error.

1
2
3
4
5
while(2 == scanf("%d %d", &n1, &n2)) {

... ... ...

}


But in C++ when I write the following:
1
2
3
4
5
while(2 == cin>>n1>>n2) {

... ... ...

}


it shows 01.cpp|9|error: no match for ‘operator==’ (operand types are ‘int’ and ‘std::basic_istream<char>::__istream_type {aka std::basic_istream<char>}’)|


What is the reason and how to do it?
not sure if this is what you need :/
1
2
3
4
	while (std::cin >> n1 >> n2) {
		if (2 == n1 || 2 == n2) continue;
                else break;
	}

Last edited on
If you don't know what something does, RTFM


http://www.cplusplus.com/reference/cstdio/scanf/
Return value: On success, the function returns the number of items of the argument list successfully filled.

http://www.cplusplus.com/reference/istream/basic_istream/operator-free/
Return value: The basic_istream object (you couldn't chain the operators otherwise)


So to check that the input was successful use
1
2
3
while( cin>>n1>>n2 ){
  //...
}
Last edited on
Actually my program is to take two values into two variables n1 and n2. And run the loop to take inputs like before until the input is entered. If user press enter without giving two inputs into two variables then the program will terminate.
that's what while continue and break does.
you may also want to make a nested while loop if one is not working ok.
Topic archived. No new replies allowed.