a beginner Q

Discussion in 'C++' started by shy, Nov 15, 2003.

  1. shy

    shy Guest

    hello,
    i a beginer in C++ programing.
    i want to print to the screen an interger number but to keep it with two
    digits long (like in a clock). how do i do it with cout function???

    exp:
    int x=5;
    cout... (???)

    output:
    05

    in C it is done like this:
    printf ("%2d",x);

    how do i do it in C++??

    tnx in advance,
    shy
    shy, Nov 15, 2003
    #1
    1. Advertising

  2. shy

    Phlip Guest

    shy wrote:

    > i a beginer in C++ programing.
    > i want to print to the screen an interger number but to keep it with two
    > digits long (like in a clock). how do i do it with cout function???


    Next time use http://groups.google.com , and scan this newsgroup for
    "iomanip".

    #include <iomanip>

    > exp:
    > int x=5;
    > cout... (???)


    cout.fill('0'); // <-- or leave out to see spaces instead of zeros

    cout << std::setw(2) << x;

    > output:
    > 05
    >
    > in C it is done like this:
    > printf ("%2d",x);


    I suspect the template here should have been "%02d" or "%02i".

    --
    Phlip
    http://www.c2.com/cgi/wiki?TestFirstUserInterfaces
    Phlip, Nov 15, 2003
    #2
    1. Advertising

  3. shy

    Jon Bell Guest

    In article <swqtb.29693$>,
    Phlip <> wrote:
    >
    >cout.fill('0'); // <-- or leave out to see spaces instead of zeros
    >
    >cout << std::setw(2) << x;


    Or, of course, you can do it in one statement with setfill():

    cout << std::setfill('0') << setw(2) << x;

    Don't forget to reset the fill character to ' ' unless you want everything
    after that point to be zero-filled!

    --
    Jon Bell <> Presbyterian College
    Dept. of Physics and Computer Science Clinton, South Carolina USA
    Jon Bell, Nov 15, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Larry Smith

    Trivial resources problem (beginner)

    Larry Smith, Oct 2, 2003, in forum: ASP .Net
    Replies:
    3
    Views:
    507
    Scott Manson
    Oct 2, 2003
  2. tripwater

    Help with Visual Studio (beginner)

    tripwater, Feb 18, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    2,451
    Amit Bahree
    Mar 9, 2005
  3. =?Utf-8?B?S3VydCBTY2hyb2VkZXI=?=

    No Class at ALL!!! beginner/beginner question

    =?Utf-8?B?S3VydCBTY2hyb2VkZXI=?=, Feb 2, 2005, in forum: ASP .Net
    Replies:
    7
    Views:
    562
    =?Utf-8?B?S3VydCBTY2hyb2VkZXI=?=
    Feb 3, 2005
  4. Rensjuh
    Replies:
    7
    Views:
    954
    Mabden
    Sep 2, 2004
  5. william nelson

    Beginner's Beginner

    william nelson, Apr 11, 2011, in forum: Ruby
    Replies:
    7
    Views:
    203
    7stud --
    Apr 12, 2011
Loading...

Share This Page