Programming Tutorials

Using cout.fill() in C++

By: Jagan in C++ Tutorials on 2007-09-17  

Normally cout fills the empty field created by a call to width() with spaces, as shown above. At times you may want to fill the area with other characters, such as asterisks. To do this, you call fill() and pass in as a parameter the character you want used as a fill character. Listing below illustrates this.

Using fill().

1:     // fill()
2:
3:     #include <iostream.h>
4:
5:     int main()
6:     {
7:        cout << "Start >";
8:        cout.width(25);
9:        cout << 123 << "< End\n";
10:
11:
12:        cout << "Start >";
13:       cout.width(25);
14:       cout.fill(`*');
15:       cout << 123 << "< End\n";
16:     return 0;
17: }

Output: Start >                  123< End
Start >******************123< End

Analysis: Lines 7-9 repeat the functionality from the previous example. Lines 12-15 repeat this again, but this time, on line 14, the fill character is set to asterisks, as reflected in the output.






Add Comment

* Required information
1000

Comments

No comments yet. Be the first!

Most Viewed Articles (in C++ )

Latest Articles (in C++)