cin unexpected behaviour on large integer input
When a large integer is provided as input, std::cin causes unexpected behaviour in the program.
std::cin unexpected behaviour when there is an integer overflow
#include <iostream>
int main() {
int cpp_is_great;
std::cin >> cpp_is_great;
int just_an_integer;
while(cpp_is_great > 0)
{
std::cin >> just_an_integer;
std::cout << "magic of c++\n";
cpp_is_great--;
}
return 0;
}
Output
10
1
magic of c++
10
magic of c++
10000000000000000000000000000000000000
magic of c++
magic of c++
magic of c++
magic of c++
magic of c++
magic of c++
magic of c++
magic of c++
scanf doesn't cause this issue, why?
This issue doesn't occur in scanf
#include <cstdio>
int main() {
int c_is_really_great;
scanf("%d",&c_is_really_great);
int just_an_integer;
while(c_is_really_great > 0)
{
scanf("%d",&just_an_integer);
printf("no magic in c\n");
c_is_really_great--;
}
return 0;
}
Output
10
1
no magic in c
10
no magic in c
10000000000000000000000000000000000000
no magic in c