Why doesn't this code work?
int x;
cin >> x;
With the input of 0x1a
I get that x == 0
and not 26
.
Why's that?
I believe in order to use hex you need to do something like this:
cin >> hex >> x;
cout << hex << x;
you can also replace hex with dec and oct etc.
Actually, You can force >>
operator to get and properly interpret prefixes 0
and 0x
. All you have to do is to remove default settings for std::cin
:
std::cin.unsetf(std::ios::dec);
std::cin.unsetf(std::ios::hex);
std::cin.unsetf(std::ios::oct);
Now, when you input 0x1a you will receive 26.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With