By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,750 Members | 1,221 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,750 IT Pros & Developers. It's quick & easy.

a beginner Q

P: n/a
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
Jul 19 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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
Jul 19 '05 #2

P: n/a
In article <sw*****************@newssvr31.news.prodigy.com> ,
Phlip <ph*******@yahoo.com> 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 <jt*******@presby.edu> Presbyterian College
Dept. of Physics and Computer Science Clinton, South Carolina USA
Jul 19 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.