Print formatting using \t and \n escape sequences – Computer Sir Ki Class

Login


Lost your password?

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

Login
[lwa]



Solved Problem #CPP#2303 siteicon   siteicon   siteicon  

Problem Statement - Print formatting using \t and \n escape sequences

Write the following text using the cout chain using \t and \n escape sequences.

Subjects        Marks
---------------------
Computers       100
Physics         95
Mathematics     98

Solution

TC++ #2303

 

Run Output

Subjects        Marks
---------------------
Computers    100
Physics 95
Mathematics  98

cout<<“Subjects\tMarks\n”
Using t we format for putting the second column after a tab. The rule is the tab counts characters from the first character of the previous word if it is less than 8 characters wide else fresh space of adjusted 8 characters is generated.. The tab is assumed to be 8 spaces for display unless changed by the output system. For Subjects it is 8 character wide so a new tab space of 8 character will be generated.
<<“———————\n”
Here we need to give dashes according to matter. 8 for subjects 8 for tab space and 5 for Marks. So total 21 dashes should suffice.

<<“Computers\t100\n”
For computers it is 9 characters wide so a space of 7 characters is generated.

<<“Physics\t\t95\n”
For physics it is 7 characters wide to first tab will only generated 1 space and an additional tab is required to generate 8 more space.

<<“Mathematics\t99\n”;
For mathematics it is 11 character wide so the tab will generate just 5 spaces.

For all independent literals we use n at the end for a line break. All the literals can be chained in the same cout chain, or we can write them as different cout instructions as well.

 

Notes

  • This kind of tabular printing is done for simple tables only. Proper formatting can be achieved using functions in the iomanip library in c++.

Common Errors

  • Correct count of letters in the word is important to fix the tab count.


Suggested Filename(s): esc-seq-fmt.cpp



Share

sunmitra| Created: 6-Jan-2018 | Updated: 14-Dec-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