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: endl stands for “end line.” It is part of the <iostream> library in C++.

  • Purpose: It serves two main purposes:

    1. Moves the cursor to the next line.

    2. Flushes the output buffer, ensuring all text is printed immediately.

 
#include <iostream>
using namespace std;
int main() {
cout << "Hello, World!" << endl;
cout << "Welcome to C++ programming!" << endl;
return 0;
}

Output:

 
Hello, World!
Welcome to C++ programming!

Difference Between endl and \n

  • \n is a newline character that moves the cursor to the next line.

  • endl also moves the cursor to the next line, but additionally flushes the output buffer.

Sub-Subheading 1.2.1: Example Showing Difference

 
#include <iostream>
using namespace std;
int main() {
cout << "Hello, World!\n";
cout << "This is using \\n" << endl;
cout << "Hello, World!" << endl;
cout << "This is using endl" << endl;

return 0;
}

Explanation:

  • \n is faster for printing multiple lines because it does not flush the buffer.

  • endl is 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.

 
#include <iostream>
using namespace std;
int main() {
for (int i = 1; i <= 5; i++) {
cout << "Number: " << i << endl;
}
return 0;
}

Output:

 
Number: 1
Number: 2
Number: 3
Number: 4
Number: 5
  • Here, endl ensures 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 SequenceMeaningExample
\nNew lineMoves text to next line
\tHorizontal tabAdds space like a tab
\\BackslashPrints \
\"Double quotePrints "
\'Single quotePrints '
\rCarriage returnMoves cursor to the beginning of line
\bBackspaceDeletes previous character

Using \n for New Lines

 
#include <iostream>
using namespace std;
int main() {
cout << "Line 1\nLine 2\nLine 3\n";
return 0;
}

Output:

 
Line 1
Line 2
Line 3
  • Each \n moves 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.

 
#include <iostream>  
using namespace std;
int main() {
cout << "Name\tAge\tClass" << endl;
cout << "Ali\t15\t10" << endl;
cout << "Sara\t14\t9" << endl;
return 0
}

Output:

 
Name   Age  Class

Ali    15   10
Sara   14   9
  • \t automatically spaces the text so it aligns in columns.

Combining \n and \t

  • You can use both together to create formatted tables and structured output.

 
#include <iostream>
using namespace std;
int main() {
cout << "Subject\tMarks\n";
cout << "Math\t95\n";
cout << "Science\t90\n";
cout << "English\t88\n";
return 0;
}

Output:

 
Subject    Marks
Math        95
Science     90
English     88
  • Here, \t aligns the marks, and \n moves each subject to the next line.

Using Escape Sequences for Quotes and Backslash

 
#include <iostream>
using namespace std;
int main() {
cout << "He said, \"C++ is fun!\"\n";
cout << "This is a backslash: \\" << endl;
return 0;
}

Output:

 
He said, "C++ is fun!"
This is a backslash: \
  • \" 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.

 
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
double num = 3.1415926535;
cout << "Default: " << num << endl;
cout << fixed << setprecision(2) << "Two decimals: " << num << endl;
cout << fixed << setprecision(5) << "Five decimals: " << num << endl;

return 0;
}

Output:

 
Default: 3.14159
Two decimals: 3.14
Five decimals: 3.14159
  • fixed ensures decimal formatting instead of scientific notation.

Aligning Numbers

  • Numbers can be aligned using setw(width).

 
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
cout << setw(10) << "Name" << setw(10) << "Score" << endl;
cout << setw(10) << "Ali" << setw(10) << 90 << endl;
cout << setw(10) << "Sara" << setw(10) << 95 << endl;
return 0;
}

Output:

 
Name  Score
Ali    90
Sara   95
  • setw() sets the width of the field, helping align columns perfectly.

Combining setw, setprecision, and fixed

 
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
double price1 = 45.678;
double price2 = 123.456;
cout << setw(10) << fixed << setprecision(2) << price1 << endl;
cout << setw(10) << fixed << setprecision(2) << price2 << endl;
return 0;
}

Output:

 
45.68
123.46
  • 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.

 
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
cout << setw(15) << "Student" << setw(10) << "Grade" << endl;
cout << setw(15) << "Ali" << setw(10) << "A+" << endl;
cout << setw(15) << "Sara" << setw(10) << "A" << endl;
return 0;
}

Output:

 
Student    Grade
Ali         A+
Sara        A
  • This creates tables that are easy to read, even for beginners.

Other Number Formatting Options

  1. Scientific Notation:

 
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
double num = 123456.789;
cout << scientific << num << endl; // 1.234568e+05
return 0;
}
  1. Showing Positive Signs:

 
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
int x = 25;
cout << showpos << x << endl; // +25
return 0;
}
  1. Internal Justification:

 
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
cout << setw(10) << internal << -123 << endl; // aligns sign and number separately
return 0;
}