Why does integer overflow cause errors with C++ iostreams?

前端 未结 3 1755
时光说笑
时光说笑 2021-01-18 17:27

Ok, so I have some problems with C++ iostreams that feels very odd, but it is probably defined behaviour, considering this happens with both MSVC++ and G++.

Say I ha

3条回答
  •  既然无缘
    2021-01-18 17:55

    a starts out with an undefined value. It's not cin's fault. Try:

    if (cin >> a) {
      cout << a endl;
    }
    

    It will check whether the read into a succeeded before using a

提交回复
热议问题