need help

Hi, I would like to know if there's a way to show at least two numbers in the output instead of just one.
For example: instead of showing 4 it shows 04.
Its for a console application.
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
    int num = 4;
    cout << setw(2) << setfill('0') << num << endl;

    return 0;
}
Thank you.
That helps alot.
examples:
cout << setfill('0') << setw(2) << 4; -> prints 04

int x = 4;
cout << setfill('0') << setw(2) << x; --> prints 04
cout << setfill('0') << setw(4) << x; --> prints 0004
cout << setfill('$') << setw(4) << x; --> prints $$$4


1. the setfill() will set what should be used as filler in case the value to be printed has lesser length compared to the set width. In this case, setfill('0') will set 0 as filler.

2. the setw() will set the width of the value to be printed. In this case, setw(2) means reserve a minimum of 2 character as a place value where to print the value to be printed.


Topic archived. No new replies allowed.