Here is an excerpt of my code, where the problem lies.
long long user_largest_plus;
long long user_largest_minus;
cout << "Input the largest+1 in decimal: " << endl;
cin >> user_largest_plus;
//cout << endl;
// cout << "Input the largest-1 in decimal: " << endl;
cin >> user_largest_minus;
cout << endl;
cout << "In decimal plus: " << user_largest_plus;
cout << endl;
cout << "In decimal minus: " << user_largest_minus;
As soon as I input 9223372036854775808 to user_largest_plus, the execution would terminate. That is, I wouldn’t be able to input for user_largest_minus.
I am using Code::Blocks, MinGW compiler.
Is it because I just overflowed the variable, and the error triggered this termination. Any work around?
By the way, that number is 2^63 - 1, maximum number that I can store.
Thanks
Try replacing
with
When the input text is not a valid
long long, two interesting things happen:user_largest_plusis never set, andbadbit is set incin.The provided code sets some value to
user_largest_plusto avoid undefined behavior, and clears the bad bit, socincan still be used.