Reputation: 131
I know that the unsigned long
is up to 4294967295 only, so here's my problem, when the user inputs many numbers (like the result is going to exceed on the limit) the converted number will be just 4294967295.
example:
Base 11: 1928374192847
Decimal: 4294967295
the result should be 5777758712535. how to fix this limit ? vb6.0 required to us. here's my code:
cout << "\t\t CONVERSION\n";
cout << "\t Base 11 to Decimal\n";
cout << "\nBase 11: ";
cin >> str;
const auto x = str.find_first_not_of("0123456789aA");
if (x != string::npos)
{
std::cout << "Invalid Input\n\n";
goto a;
}
unsigned long x = strtoul(str.c_str(), NULL, 11);
cout << "Decimal: " << x << "\n\n";
The program should say "out of range" if the result will exceed 4294967295. Sorry im just a beginner.
Upvotes: 0
Views: 152
Reputation: 468
From the strtoul docs on www.cplusplus.com :
If the value read is out of the range of representable values by an unsigned long int, the function returns ULONG_MAX (defined in <climits>), and errno is set to ERANGE.
You should check the errno to determine if the value was out of range.
Upvotes: 2