Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > a beginner Q

Reply
Thread Tools

a beginner Q

 
 
shy
Guest
Posts: n/a
 
      11-15-2003
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


 
Reply With Quote
 
 
 
 
Phlip
Guest
Posts: n/a
 
      11-15-2003
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


 
Reply With Quote
 
 
 
 
Jon Bell
Guest
Posts: n/a
 
      11-15-2003
In article <swqtb.29693$(E-Mail Removed)> ,
Phlip <(E-Mail Removed)> 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 <(E-Mail Removed)> Presbyterian College
Dept. of Physics and Computer Science Clinton, South Carolina USA
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Beginner's Beginner william nelson Ruby 7 04-11-2011 11:23 PM
Beginner can't get a wireless VPN connect? Russell DeMarco Wireless Networking 2 03-18-2005 10:35 PM
Beginner can't connect Laptop to wireless network Russell DeMarco Wireless Networking 2 03-17-2005 03:31 PM
No Class at ALL!!! beginner/beginner question =?Utf-8?B?S3VydCBTY2hyb2VkZXI=?= ASP .Net 7 02-03-2005 02:47 PM
Tutorial for beginner/ Tutorial voor beginner Rensjuh C++ 7 09-02-2004 12:41 AM



Advertisments