Print formatting using \t and \n escape sequences – CSKC
Sign In

Search

Solved Problem #CPP#2303    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

 

×

Ask AI About This Code

Default Prompt that is passed along with the code is given below, You can write your own prompt if you so desire.



×

Test CPP

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

Created: 6-Jan-2018 21:25 | Updated: 14-Dec-2018 00:29|