Use of setw() manipulator – Computer Sir Ki Class

Login


Lost your password?

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

Login
[lwa]



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

Hello
Hello
Hello
Hello
3.1416
3.1416

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
cout<<setw(5)<<str<<endl;
Since the string is of 5 characters setting display width to 5 will not have any effect.

cout<<setw(6)<<str<<endl;
Now the width is set to 1 more than given string width so the string will be printed after leaving 1 space.

cout<<setw(7)<<str<<endl;
Here the string hello will be printed after leaving 2 spaces.

cout<<str<<endl;
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.
cout<<f<<endl;
This is number printing from the left most column.

cout<<setw(10)<<f<<endl;
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.

Notes


Suggested Filename(s): setw.cpp



Share

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


Back