#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <limits>
void do_without_exceptions()
{
std::istringstream in("3 4\n5 6\nbad line\n10 39\n100 1001\nbad line");
while (in.good())
{
int a, b;
while (in >> a >> b)
std::cout << a << " + " << b << " = " << a + b << '\n';
if (!in)
{
in.clear();
in.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
}
}
void do_with_exceptions()
{
auto exception_bits = std::istringstream::badbit |
std::istringstream::failbit |
std::istringstream::eofbit;
std::istringstream in("3 4\n5 6\nbad line\n10 39\n100 1001\nbad line");
in.exceptions(exception_bits);
while (in.good())
{
try {
int a, b;
in >> a >> b;
std::cout << a << " + " << b << " = " << a + b << '\n';
}
catch (std::istream::failure& ex)
{
if (in.fail())
{
try {
in.clear();
in.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
catch (std::istream::failure&)
{
}
}
}
}
}
int main(int argc, char* argv[])
{
do_without_exceptions();
do_with_exceptions();
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8ZnN0cmVhbT4KI2luY2x1ZGUgPHN0cmluZz4KI2luY2x1ZGUgPHNzdHJlYW0+CiNpbmNsdWRlIDxsaW1pdHM+Cgp2b2lkIGRvX3dpdGhvdXRfZXhjZXB0aW9ucygpCnsKICAgIHN0ZDo6aXN0cmluZ3N0cmVhbSBpbigiMyA0XG41IDZcbmJhZCBsaW5lXG4xMCAzOVxuMTAwIDEwMDFcbmJhZCBsaW5lIik7CgogICAgd2hpbGUgKGluLmdvb2QoKSkKICAgIHsKICAgICAgICBpbnQgYSwgYjsKICAgICAgICB3aGlsZSAoaW4gPj4gYSA+PiBiKQogICAgICAgICAgICBzdGQ6OmNvdXQgPDwgYSA8PCAiICsgIiA8PCBiIDw8ICIgPSAiIDw8IGEgKyBiIDw8ICdcbic7CgogICAgICAgIGlmICghaW4pCiAgICAgICAgewogICAgICAgICAgICBpbi5jbGVhcigpOwogICAgICAgICAgICBpbi5pZ25vcmUoc3RkOjpudW1lcmljX2xpbWl0czxzdGQ6OnN0cmVhbXNpemU+OjptYXgoKSwgJ1xuJyk7CiAgICAgICAgfQogICAgfQp9Cgp2b2lkIGRvX3dpdGhfZXhjZXB0aW9ucygpCnsKCWF1dG8gZXhjZXB0aW9uX2JpdHMgPSBzdGQ6OmlzdHJpbmdzdHJlYW06OmJhZGJpdCB8CgkgICAgICAgICAgICAgICAgICAgICAgc3RkOjppc3RyaW5nc3RyZWFtOjpmYWlsYml0IHwKCSAgICAgICAgICAgICAgICAgICAgICBzdGQ6OmlzdHJpbmdzdHJlYW06OmVvZmJpdDsKCQogICAgc3RkOjppc3RyaW5nc3RyZWFtIGluKCIzIDRcbjUgNlxuYmFkIGxpbmVcbjEwIDM5XG4xMDAgMTAwMVxuYmFkIGxpbmUiKTsKICAgIGluLmV4Y2VwdGlvbnMoZXhjZXB0aW9uX2JpdHMpOwoKICAgIHdoaWxlIChpbi5nb29kKCkpCiAgICB7CiAgICAgICAgdHJ5IHsKICAgICAgICAgICAgaW50IGEsIGI7CiAgICAgICAgICAgIGluID4+IGEgPj4gYjsKICAgICAgICAgICAgc3RkOjpjb3V0IDw8IGEgPDwgIiArICIgPDwgYiA8PCAiID0gIiA8PCBhICsgYiA8PCAnXG4nOwogICAgICAgIH0KCiAgICAgICAgY2F0Y2ggKHN0ZDo6aXN0cmVhbTo6ZmFpbHVyZSYgZXgpCiAgICAgICAgewogICAgICAgICAgICBpZiAoaW4uZmFpbCgpKQogICAgICAgICAgICB7CiAgICAgICAgICAgICAgICB0cnkgewogICAgICAgICAgICAgICAgICAgIGluLmNsZWFyKCk7CiAgICAgICAgICAgICAgICAgICAgaW4uaWdub3JlKHN0ZDo6bnVtZXJpY19saW1pdHM8c3RkOjpzdHJlYW1zaXplPjo6bWF4KCksICdcbicpOwogICAgICAgICAgICAgICAgfQoKICAgICAgICAgICAgICAgIGNhdGNoIChzdGQ6OmlzdHJlYW06OmZhaWx1cmUmKQogICAgICAgICAgICAgICAgewogICAgICAgICAgICAgICAgfQogICAgICAgICAgICB9CiAgICAgICAgfQogICAgfQp9CgoKaW50IG1haW4oaW50IGFyZ2MsIGNoYXIqIGFyZ3ZbXSkKewogICAgZG9fd2l0aG91dF9leGNlcHRpb25zKCk7CiAgICBkb193aXRoX2V4Y2VwdGlvbnMoKTsKfQ==