Thread: leading zero on single digit int

  1. #1
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002

    leading zero on single digit int

    Is there a way to include a leading zero on a single digit int using cout without using if statements?

    edit: nevermind, if statements are sufficiently easy
    Last edited by confuted; 07-31-2003 at 03:29 PM.

  2. #2
    Registered User
    Join Date
    Jul 2003
    In case you still wanted to know, the printf statement can do what you want. You'd have to specify the precision ( I believe it is .#, that is, a period followed by a number). In the case of integers it will force the output to have a specific minimum number of digits and print leading zeros if necessary. I found that info on the following page:


  3. #3
    Registered User
    Join Date
    Oct 2001
    #include <iostream>
    #include <iomanip>
    using namespace std;
    int main(void)
       int a = 3;
       cout << setw(2) << setfill('0') << a << endl;
       return 0;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to combine these working parts??
    By transgalactic2 in forum C Programming
    Replies: 0
    Last Post: 02-01-2009, 08:19 AM
  2. Working with random like dice
    By SebastionV3 in forum C++ Programming
    Replies: 10
    Last Post: 05-26-2006, 09:16 PM
  3. Game Won't Compile
    By jothesmo in forum C++ Programming
    Replies: 2
    Last Post: 04-01-2006, 04:24 PM
  4. Converted from Dev-C++ 4 to Dev-C++ 5
    By Wraithan in forum C++ Programming
    Replies: 8
    Last Post: 12-03-2005, 07:45 AM
  5. Binary Search Trees Part III
    By Prelude in forum A Brief History of
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM