Find the answer to your Linux question:
Results 1 to 3 of 3
Hey all. Again, another silly question from my attempts to learn C++. I am on a chapter that is explaining the setf function. I have copied this following code directly ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Linux Guru Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,252

    C++ setf function


    Hey all. Again, another silly question from my attempts to learn C++.

    I am on a chapter that is explaining the setf function. I have copied this following code directly from the book:
    Code:
    #include <iostream>
    using namespace std;
    
    int main&#40;&#41;
    &#123;
        int number = 0x3FF;
    
        cout << "Dec&#58; " << number << endl;
    
        cout.setf&#40;ios&#58;&#58;hex&#41;;
        cout << "Hex&#58; " << number << endl;
    
        cout.setf&#40;ios&#58;&#58;dec&#41;;
    
        return 0;
    &#125;
    According to my book, the output should be:
    Code:
    Dec&#58; 1023
    Hex&#58; 3ff
    However, when I run it, my output is:
    Code:
    Dec&#58; 1023
    Hex&#58; 1023
    Can anyone explain what I'm doing wrong?


    Also, kinda unrelated, I'm using O'Reilly's "Practical C++ Programming". Is there a better book out there? Because this one just isn't cutting it. Preferably with end-of-chapter code-writing questions and answers to those questions in the back.

  2. #2
    Linux Guru AlexK's Avatar
    Join Date
    Feb 2005
    Location
    Earth
    Posts
    3,379
    from my very limited knowledge of c++, you are supposed to activate the base before printing it out:
    Code:
    #include <iostream> 
     using namespace std; 
     
     int main&#40;&#41; 
     &#123; 
    
         int number = 0x3FF; 
      cout.setf&#40;ios&#58;&#58;dec&#41;;
     
         cout << "Dec&#58; " << number << endl; 
     
         cout.setf&#40;ios_base&#58;&#58;hex, ios_base&#58;&#58;basefield&#41;;  // set hex as basefield
         cout.setf &#40; ios_base&#58;&#58;showbase &#41;;  // use this line to activate the base for you
         cout << "Hex&#58; " << number << endl; 
         cout.setf &#40; 0, ios_base&#58;&#58;showbase &#41;;               // deactivate base
         return 0; 
     &#125;
    Now I haven't read the O'Reilly's book on C++, but for learning programming, I usually use the Schaum's series of books, it has end of chapter questions as well as answers in some cases.
    Life is complex, it has a real part and an imaginary part.

  3. #3
    Linux Guru Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,252
    Aha!

    Well, you only need the 'showbase' thingy if you want the "0x" to show up (which I happen to do, so thanks). But the book didn't mention anything about basefield.

    Righto then. Thanks. I think I'll finish this book up to get out of it what I can, but this is definitely not a good enough book.

  4. $spacer_open
    $spacer_close

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •