C++ Input and Output | cin, cout, endl vs \n, iomanip and File Streams


This complete tutorial on C++ Input and Output explains how to read input and display output in C++ programs. It covers the use of cin and cout, the difference between endl and \n, input/output formatting using the iomanip library, and the basics of file streams. The tutorial follows best coding practices and helps beginners write clean, readable, and efficient C++ programs.

Input and Output – Complete Tutorial

1. Input and Output in C++

C++ provides stream-based input and output using the <iostream> header.

Streams represent a flow of data from a source to a destination.

  1. cin → standard input (keyboard)
  2. cout → standard output (console)

2. Using cin for Input

cin is used to read data from the standard input.

Example:


int age;
cin >> age;

Multiple inputs:


int a, b;
cin >> a >> b;

Best Practices:

  1. Validate user input where required
  2. Use meaningful prompts
  3. Avoid mixing cin with C-style input (scanf)

3. Using cout for Output

cout is used to display output on the console.

Example:


cout << "Welcome to C++";

Multiple outputs:


cout << "Sum = " << a + b;

4. endl vs \n

Both endl and \n are used to insert a new line.

Using endl


cout << "Hello" << endl;

Using \n


cout << "Hello\n";

Difference:

Featureendl\n
Inserts new lineYesYes
Flushes output bufferYesNo
PerformanceSlowerFaster

Best Practice:

  1. Use \n for better performance
  2. Use endl only when buffer flushing is required

5. Input/Output Formatting (iomanip)

The <iomanip> library is used to format input and output.

Common Formatting Functions:

setw


#include <iomanip>
cout << setw(10) << 123;

setprecision


cout << fixed << setprecision(2) << 3.14159;

left and right


cout << left << setw(10) << "Name";
cout << right << setw(10) << "Score";

setfill


cout << setfill('*') << setw(10) << 50;

Best Practices:

  1. Use formatting for reports and tables
  2. Reset formatting when required
  3. Avoid over-formatting simple output

6. File Stream Basics

C++ provides file handling using file streams from the <fstream> header.

File Stream Classes:

  1. ifstream → input from file
  2. ofstream → output to file
  3. fstream → input and output

Writing to a File


#include <fstream>

ofstream file("data.txt");
file << "Hello File";
file.close();

Reading from a File


#include <fstream>
#include <string>

ifstream file("data.txt");
string line;
getline(file, line);
file.close();

Best Practices for File Handling:

  1. Always check if file opened successfully
  2. Close files after use
  3. Handle file errors gracefully

Example:


if (!file.is_open()) {
cout << "Error opening file";
}

Common Mistakes to Avoid

  1. Forgetting to include <iostream> or <fstream>
  2. Using endl excessively
  3. Not closing file streams
  4. Ignoring file open errors

Summary

In this chapter, you learned how to handle input and output in C++, including cin, cout, endl vs \n, output formatting using iomanip, and the basics of file streams. These concepts are essential for building interactive and file-based C++ applications.