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 have this program:
#include <iostream>
using namespace std;
int main()
{
int a;
cin >> a;
cout << a << endl;
cin >> a;
cout << a << endl;
return 0;
}
If I intentionally overflow by giving the first cin a value that is larger than the max limit of an int, all further calls to cin.operator>>()
will immediately return for some reason, and a
is set to some value. The value seems to be undefined.
Why, and where is this behavior documented? Is there a way to figure out if such an overflow occured?
Also, this similar program seems to work as I intend. If I overflow the value, it will give a
some value, and continue on as if the overflow never happened.
#include <cstdio>
using namespace std;
int main()
{
int a;
scanf("%d", &a);
printf("%d\n", a);
scanf("%d", &a);
printf("%d\n", a);
scanf("%d", &a);
printf("%d\n", a);
return 0;
}