Create a free account on Computer Sir Ki Class and unlock access to valuable learning resources.
Exam Paper Downloads: After registering and logging in, you can download exam question papers in PDF format to practice and prepare for your exams.
Student Support: Get assistance with your doubts, queries, and questions related to your studies. Our support is here to help you understand topics and resolve your learning difficulties.
Register today and make your exam preparation easier and more effective!
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
The code has been copied automatically for you in the clipboard. After that open any of the service and press cntrl-v to paste in the given code area. Needs a bit of tweaking as per the link.
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.