Input and Output (I/O) in C++ are fundamental skills that every programmer must master. I/O formatting allows you to control how data is displayed or read, making your programs more readable, professional, and user-friendly. This lesson focuses on managing output with cout, handling new lines and tabs, and formatting numbers for clarity.
endl in C++
The endl keyword in C++ is used to insert a newline character and flush the output buffer. It is often used when we want the output to move to the next line or to ensure that all output appears immediately on the screen.
Understanding endl
Definition:
endlstands for “end line.” It is part of the<iostream>library in C++.Purpose: It serves two main purposes:
Moves the cursor to the next line.
Flushes the output buffer, ensuring all text is printed immediately.
Output:
Difference Between endl and \n
\nis a newline character that moves the cursor to the next line.endlalso moves the cursor to the next line, but additionally flushes the output buffer.
Sub-Subheading 1.2.1: Example Showing Difference
Explanation:
\nis faster for printing multiple lines because it does not flush the buffer.endlis preferred when you want immediate output, such as in real-time logs or interactive programs.
Using endl in Loops
endl is very useful when printing multiple lines in loops, such as tables or lists.
Output:
Here,
endlensures that each number is printed on a separate line, making the output clean and readable.
Escape Sequences (\n, \t, etc.)
Escape sequences in C++ are special characters used to format output in a specific way. They begin with a backslash \ followed by a letter.
Common Escape Sequences
| Escape Sequence | Meaning | Example |
|---|---|---|
\n | New line | Moves text to next line |
\t | Horizontal tab | Adds space like a tab |
\\ | Backslash | Prints \ |
\" | Double quote | Prints " |
\' | Single quote | Prints ' |
\r | Carriage return | Moves cursor to the beginning of line |
\b | Backspace | Deletes previous character |
Using \n for New Lines
Output:
Each
\nmoves the output to the next line.Useful for printing lists, tables, and paragraphs.
Using \t for Tabs
Tabs are useful for aligning text, like in tables or columns.
Output:
\tautomatically spaces the text so it aligns in columns.
Combining \n and \t
You can use both together to create formatted tables and structured output.
Output:
Here,
\taligns the marks, and\nmoves each subject to the next line.
Using Escape Sequences for Quotes and Backslash
Output:
\"prints double quotes.\\prints a backslash.Escape sequences help avoid syntax errors while formatting strings.
Formatting Numbers
In C++, you can format numbers to make them look neat for output, especially in financial calculations, scientific data, and tables.
Setting Decimal Precision
Use the
<iomanip>library for number formatting.setprecision(n)sets the number of decimal places.
Output:
fixedensures decimal formatting instead of scientific notation.
Aligning Numbers
Numbers can be aligned using
setw(width).
Output:
setw()sets the width of the field, helping align columns perfectly.
Combining setw, setprecision, and fixed
Output:
This ensures numbers are neatly aligned and rounded to 2 decimal places.
Using setw with Strings
You can also align text strings for professional output.
Output:
This creates tables that are easy to read, even for beginners.
Other Number Formatting Options
Scientific Notation:
Showing Positive Signs:
Internal Justification: