, x y -, '|' while. , , .
, , int, cin >> something , , .
(, |), cin >> , , .
, , , | , , ad infinitum - : )
, , , , |. , . , , if (stream >> variable).
cin.peek(), , cin.get(), . , peek, get , operator>>.
- :
#include <iostream>
#include <cctype>
int main() {
int x, y;
while (true) {
while (std::isspace(std::cin.peek())) std::cin.get();
if (std::cin.peek() == '|') break;
if (! (std::cin >> x >> y)) {
std::cout << "Invalid input, not two integers\n";
break;
}
std::cout << "You entered " << x << " and " << y << "\n";
}
return 0;
}
, ( ):
pax$ ./myprog </dev/null
Invalid input, not two integers
pax$ echo hello | ./myprog
Invalid input, not two integers
pax$ echo 1 | ./myprog
Invalid input, not two integers
pax$ echo 1 2 | ./myprog
You entered 1 and 2
Invalid input, not two integers
pax$ printf '1 2|' | ./myprog
You entered 1 and 2
pax$ printf '1 2\n3 4\n5 6 7 8 \n|' | ./myprog
You entered 1 and 2
You entered 3 and 4
You entered 5 and 6
You entered 7 and 8
pax$ printf '1 10 11 12 13 14 | ' | ./myprog
You entered 1 and 10
You entered 11 and 12
You entered 13 and 14