setf method for left and right alignment in a display field – Computer Sir Ki Class

Login


Lost your password?

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

Login
[lwa]



Code Learning #CPP#4318    siteicon   siteicon  

setf method for left and right alignment in a display field

Use of setf method of cout to align content as left or right in a display field.

Learning Objectives

  • Learning setf to align content within there fields in a left and right aligned fashion.
  • Learning to unset the alignment formatting.
  • Learning the persistent bahaviour of setf method.

Source Code

TC++ #4318

 

Source Code

Run Output

Hello     Hello
Hello Hello
Hello Hello

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.setf(ios::left);
This will set format for all subsequent display fields to aligned left in there field area. By default the content is aligned to right in its respective field area.

cout<<setw(10)<<str<<setw(10)<<str<<endl;
Hello will be printed twice with both field set to size of 10 and content aligned to left. Notice that setf is persistent in nature and it is not required to be set again and again for every field.

cout.unsetf(ios::left);
Since setf is persistent in nature we need to unset it to come to normal default status.
cout<<setw(10)<<str<<setw(10)<<str<<endl;
This will print the content as default way of fitting content towards the right of the given field width.

cout.setf(ios::right);
This is done for right alignment of content. This is useful when tabulated entry has to be printed.

cout<<setw(10)<<str<<setw(10)<<str<<endl;
This will print the content in right aligned form.

Common Errors

  • Commonly learners forget to set has a persistent behaviour unlike setw.


Suggested Filename(s): setwsetf.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 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 Mult-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