Thread: Leading astray? (01 - 09)

  1. #1
    Unregistered
    Guest

    Question Leading astray? (01 - 09)

    Hi all,

    How do I display leading zero in single integer like 1-9 becomes 01-09? Are there any function or library to use? Thanks for enlightenments. My compiler Dev- C++ 4.

  2. #2
    Registered User
    Join Date
    Dec 2001
    Posts
    32
    How about prefixing a 0 if int / 10 < 1 ?

  3. #3
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    Code:
    #include<iostream>
    using namespace std;
    int main()
    {
    for ( int i=1;i<15;++i)
    cout << ((i<10) ? "0":"") << i <<endl;
    return 0;
    }
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  4. #4
    Unregistered
    Guest

    Smile

    Aahh... easy when you know how, thanks Stone_Coder and Seron for your reply to such a newbie question. Thanks!

  5. #5
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    ..and another solution -

    Code:
    #include <iostream>
    #include <iomanip>
    
    using namespace std;
    
    
    int main()
    {
    
    	cout << setfill('0') << setw(2) << 1;
    
    	return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. compare function ignoring leading white space
    By mc72 in forum C Programming
    Replies: 5
    Last Post: 11-23-2008, 01:33 PM
  2. Removing Leading & Trailing Whitespace
    By EvilGuru in forum C Programming
    Replies: 11
    Last Post: 12-01-2005, 02:59 PM
  3. Resource ICONs
    By gbaker in forum Windows Programming
    Replies: 4
    Last Post: 12-15-2003, 07:18 AM
  4. Bitunwise
    By Azmeos in forum C++ Programming
    Replies: 28
    Last Post: 07-10-2003, 11:56 AM
  5. having difficulty with this problem
    By Unregistered in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 03-28-2002, 11:09 PM