a beginner Q

S

shy

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
 
P

Phlip

shy said:
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 said:
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".
 
J

Jon Bell

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!
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top