Use of setw() manipulator – Computer Sir Ki Class


Lost your password?

Don't have an account ?
Register (It's FREE) ×


Code Learning #CPP#4310    siteicon   siteicon  

Use of setw() manipulator

Demonstration of setw manipulation for setting display width

Learning Objectives

  • Learning to use setw io manipulator to set field width for text as well as numbers.

Source Code

TC++ #4310


Source Code

Run Output


Code Understanding

#include <iomanip>
This include file is required for all IO manipulation for display purposes.

char str[]=”Hello”;
This string will be used for display
Since the string is of 5 characters setting display width to 5 will not have any effect.

Now the width is set to 1 more than given string width so the string will be printed after leaving 1 space.

Here the string hello will be printed after leaving 2 spaces.

Here the string will again be printed from start of the line. This also demonstrates that previous effect of setw will not be there.

float f=3.1416;
This decimal number will be used to test the affect of setw on numbers.
This is number printing from the left most column.

Since the number is of 6 digits including the decimal so it will start printing after leaving first 4 columns as the width has been set to 10 columns.


Suggested Filename(s): setw.cpp


sunmitra| Created: 15-May-2018 | Updated: 15-Sep-2018|

Introductory Sessions Beginning to Program Tokens Keyword and Identifiers Data Types Variables and Constants Operators Simple User Input Building Expressions and Formulas Simple Real World Problems Simple If and If Else Multiple-Nested-Ladder of If Else Switch case selection Simple Loops Tricks in Loops - break continue scope Loop Applications - Handling numerals Series printing loops Nested Loops Pattern printing loops Number Varieties and Crunches String Handling (Null Terminated) Strings - string class type Functions (Built-in) Functions - user defined Functions Reference Passing/Returning Arrays Concepts and 1-D Arrays Array Data Management Two dimensional arrays and Matrices Structures Basics Structures passing/returning 2D Array Memory Addressing Display Using IO Manipulation Display Using C Formatting Tricks User Defined Data Types Enumerated Types Preprocessor Directives And Macros Exception Handling Programming Paradigms and OOPs Advantages Abstraction and Encapsulation Polymorphism Inheritance Function Overloading Concepts Function Overloading Varieties Function Overloading Special Cases Defining Classes Creating and Using Class Objects Class Members Accessibility Class Function Types Inline Functions Constant Functions Nesting of Functions Class Members Scope Resolution Static Members in a Class Array of Objects Constructor Concepts Default Constructor Parameterized Constructor Copy Constructor Constructor Overloading Destructors Inheritance Fundamentals Public Derivations Private and Protected Derivations Multiple Inheritance Multi-Level Inheritance Class Nesting Data File Concepts Handling Text Files Handling Binary Files Pointer Concepts Pointer and Arrays Pointers and Functions Object Pointers This Pointer Linked Lists Stacks Queues