0

something weird is happening to my program. I am currently using lots of threads in my program, and will not be feasible to paste everything here.

However this is my problem:

int value = 1000;
std::cout << value << std::endl;
//output:  3e8

Any idea why is my output 3e8?

Whats the command to fix it back to print decimal values?

Thanks in advance! :)

mister
  • 3,303
  • 10
  • 31
  • 48
  • 2
    You should try to reduce your code to a minimal example that exposes your behavior. As it stands, there is not enough information to help you. – Björn Pollex Aug 28 '12 at 11:53
  • 2
    The value is correct: 0x3E8 = 1000. What is your question? – Daniel Hilgarth Aug 28 '12 at 11:53
  • Is there any way to fix all your values to print out in hex? then this way i can go find the root of the problem. – mister Aug 28 '12 at 11:54
  • 1
    You may use `printf("%x\n", value);` for hex and `printf("%d\n", value);` for dec if you want ad-hoc control over output format. – Agent_L Aug 28 '12 at 11:55

3 Answers3

11

Some other thread changed the default output radix of the std::cout stream to hexadecimal. Note that 100010 = 3e816, i.e. 1000 == 0x3e8.

Daniel Hilgarth
  • 171,043
  • 40
  • 335
  • 443
unwind
  • 391,730
  • 64
  • 469
  • 606
6

Somewhere in your program a call such as:

std::cout << std::hex << value;

has been used. To revert output to normal (decimal) use:

std::cout << std::dec;

here is a relevent link to the different ways numbers can be output on std::cout.

Also, as pointed out in the comments below, the standard method of modifying cout flags safely appears to be the following:

ios::fmtflags cout_flag_backup(cout.flags()); // store the current cout flags

cout.flags ( ios::hex ); // change the flags to what you want

cout.flags(cout_flag_backup); // restore cout to its original state

Link to IO base flags


As stated in the comments below, it would also be wise to point out that when using IO Streams it is a good idea to have some form of synchronisation between the threads and the streams, that is, make sure no two threads can use the same stream at one time.
Doing this will probably also centralise your stream calls, meaning that it will be far easier to debug something such as this in the future.
Heres an SO question that may help you

Community
  • 1
  • 1
Serdalis
  • 10,296
  • 2
  • 38
  • 58
  • 1
    Glad I could help, Try searching for any places in your code that have the first line, and add `<< std::dec` after the value, that will fix this issue. Oh and please mark as answer if this was the fix so others will know this has issue has been solved :) – Serdalis Aug 28 '12 at 12:01
  • 1
    yeah sure! i could not mark it previously due to time limit. :) – mister Aug 28 '12 at 12:21
  • @downvoter, please give a reason when down voting an answer. I cannot see what could be wrong with this answer but if there is something wrong I would like to know so I can fix it. – Serdalis Aug 28 '12 at 13:26
  • 1
    @Serdalis While I certainly don't think it worth a downvote, the answer isn't really complete. The usual solution is to save the flags before modifying them, and restore them when finished. (Also, since the OP mentioned threading, it's worth pointing out that using `std::cout` in more than one thread requires synchronization.) – James Kanze Aug 28 '12 at 13:50
  • @JamesKanze thanks for the info, I wasn't aware of that technique, seems much more stable than the inline modifications. – Serdalis Aug 31 '12 at 13:01
  • @Serdalis I tend to use both: braces and suspenders, if you want. Whenever I change any formatting, I save the state and restore it (using RAII). But unless I'm the sole owner of the code (and so can be sure that the stream is in its default state), I'll explicitly set all of the flags I need. – James Kanze Aug 31 '12 at 14:22
3

Chances are that another thread has changed the output to hex on cout. I doubt that these streams are thread-safe.

Christian Stieber
  • 9,954
  • 24
  • 23
  • 2
    it doesn't have to be another thread. it's easy enough to do std::hex then forget to do std::dec afterwards to revert it. – Tom Tanner Aug 28 '12 at 12:08
  • @TomTanner It's not if you do it right. Anytime you modify any of the flags, you should guard the modification, ensuring that the original values are restored before leaving the block. – James Kanze Aug 28 '12 at 13:51
  • @JamesKanze that was more or less what I was saying. Easy enough to make the mistake – Tom Tanner Aug 28 '12 at 14:56
  • @TomTanner Question of habit, I guess. Every place I've worked, it has been standard practice to create an instance of `IOSave` (or whatever the class was known as locally) at the top of any block which did output. (In a multithreaded environment, this class could also handle locking. But you have to be careful about implicit locks; it's very easy to get a mutual deadlock.) – James Kanze Aug 28 '12 at 15:14