Click here to Skip to main content
15,888,242 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
Hi Guys,

I have a quick question once again and that is how to add 0 infront of the integar even if the user don't enter it e.g. user enters 3,4,5,6 and it prints as 03,04,05,06 but not when user enters 10,11,12,13 as 010,011,012,013 etc.

Much appreciate your help and suggestions in advance.

Cheers
Posted
Comments
Aescleal 11-Jul-12 1:54am    
Have a 5 to balance out whoever gave you a 1 - this seems like a legit enquiry to me as formatted I/O is a bit of a bugger to get your head around in C or C++.

printf ("%02d", 5);
printf ("%02d", 13);
 
Share this answer
 
Comments
Salman Ali Hero 10-Jul-12 10:16am    
cheers mate, working all good
nv3 10-Jul-12 10:29am    
You're welcome!
A good idea before asking basic questions like this is to check ther documentation[^] first. That has the double benefit of getting the answer more quickly, and also learning other facts and features that you may need in the future.
 
Share this answer
 
Comments
Salman Ali Hero 12-Jul-12 0:04am    
Thanks Richard,

For your kind suggestion.

Cheers
nv3's answer is the way to go if you're using C.
C++
std::cout << std::setw( 2 ) << std::setfill( '0' ) <<  5 << std::endl;
std::cout << std::setw( 2 ) << std::setfill( '0' ) << 25 << std::endl;
is the C++ way. If you're using C++ then don't use printf, it's not the safest way of doing output in the world.
 
Share this answer
 
Comments
Salman Ali Hero 10-Jul-12 10:25am    
Thanks for your answer but actually I am writing a program in C
Aescleal 10-Jul-12 10:29am    
Your question had C and C++ tags, might be an idea in future to check them before posting. I'll leave the solution here though 'cause it provides a contrast between the C and ++ style and it might help someone else in the future.
Salman Ali Hero 12-Jul-12 0:06am    
yeah thanks for tht, I was thinking about getting familier with C++ aswell but never tried :-0
pasztorpisti 10-Jul-12 10:50am    
+5 because this is also a good answher, however I rarely seen c++ streams even in production code. Lot of ppl don't like this kind of abuse of operator overloading. :D
Aescleal 10-Jul-12 12:36pm    
Just to provide a bit of balance (thanks for the 5 BTW)...

- I've personally used streams almost exclusively for I/O since 1999
- Every project I've worked on since 2004 has used them extensively (even in code to control hard disks, games, security, futures trading)
- They work on just about any hosted environment (including modern games consoles) and a fair number of embedded ones
- They're a superb abstraction when you have to substitute I/O facilities when unit testing
- They're incredibly customisable; add boost and it becomes trivial
- Very little chance of making a type snafu

About the only downside is that their syntax is a bit cumbersome. And as for operator overloading these days I tend to think the abuse is using the insertion and extraction operators for bit shifting.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900