21

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?

Mat
  • 202,337
  • 40
  • 393
  • 406
Quaker
  • 1,483
  • 3
  • 20
  • 36

5 Answers5

37

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.

sean
  • 510
  • 7
  • 12
27

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.

Karol D
  • 371
  • 3
  • 5
  • 5
    I think this should be the accepted answer. With hex if you do something like: cin >> hex > x; and input 17 you will get 23 which is almost always not what you want as a user. With this method you get what any user want, prefix are taken in consideration. 23 gives 23, 0xa gives 0xa and so on. – Tic Jul 27 '17 at 09:03
  • 3
    They are both good answers, and it really depends on the application context to determine which you should use. Stroustrup goes over both in his book: both methods are useful to know. Both upvoted. Question upvoted. Everyone gets an upvote! – eric May 20 '18 at 14:31
  • 3
    `std::cin.unsetf(std::ios::basefield);` – Marek R Mar 15 '19 at 09:57
3

Think of << and >> when using std::cout/std::cin like so:

std::cout << x means get the value from x

std::cin >> x means put the value into x

Notice the directions in which the operators are pointing. That should give you a hint as to what they do when using these functions.

The reason that you are getting 0 as a result and not 26 is because std::cin will parse the all non numeric characters from your input. After all, x is an int, it won't recognize 0x as a part of a hexadecimal number. It would of had the same behavior if the input was 9x2 (the result would simply be 9).

David G
  • 94,763
  • 41
  • 167
  • 253
1

Your code should read:

int x;
cin >> hex >> x;

By default cin will expect any number read in to be decimal. Clearly, 0x1a is not a valid decimal and so the conversion cannot take place. To get it to work we have to use the stream modifier hex which prompts cin to expect number conversion from hexadecimal rather than decimal.

The 0x prefix is optional in this case so the input 10 would be read and stored as decimal 16.

Component 10
  • 10,247
  • 7
  • 47
  • 64
  • 2
    Can you explain to the OP why? – Jamie Keeling Nov 02 '12 at 14:13
  • Okay, but how would one ask `cin` to read hex instead? Is there a flag, as there is for `cout`? – Translunar Apr 08 '14 at 19:41
  • There is no way that I know of other than than using the `std::hex` manipulator to set the basefield format flag on a stream. You can either use `operator<<` or `setf` on the stream to do this. By default, when `cin` is created, this is set to `std::dec`. This is the same for `std::cout` – Component 10 Apr 10 '14 at 09:22
0
#include<iostream>
using namespace std;

int main()
{
    int data[16];
    cout << "enter the 16 hexadecimal numbers\n";
    for(int i = 0;i < 16;i++)
    {
        if(cin >> hex >> data[i])
            cout << "input worked\n";
        else{
            cin.clear();
            cout << "invalid input\n";
        }
    }
}
AlbertFG
  • 157
  • 13