C++ iostream
Input and output operations in C++
📺 What is iostream?
The iostream library provides input and output functionality in C++. It includes cout for output, cin for input, and other stream objects for console operations and data formatting.
#include <iostream>
using namespace std;
int main() {
cout << "Hello World!" << endl;
return 0;
}
Output:
Hello World!
iostream Components
cout
Standard output stream for displaying data
cout << "Hello!" << endl;
cin
Standard input stream for reading data
int age;
cin >> age;
cerr
Standard error stream for error messages
cerr << "Error occurred!" << endl;
clog
Standard log stream for logging information
clog << "Log message" << endl;
🔹 Basic Output with cout
The cout object, part of the standard output stream, is the primary tool for displaying
information to the console in C++. Used with the insertion operator (<<), it can
output literals, variables, and expressions, and supports chaining multiple items in a single statement. It's
essential for debugging, providing user feedback, and presenting program results. For more complex formatting,
manipulators like endl (to insert a newline and flush the buffer) or functions from the
<iomanip> library can be used with cout to control the appearance of text, numbers,
and spacing.
#include <iostream>
using namespace std;
int main() {
// Simple output
cout << "Welcome to C++!" << endl;
// Multiple values
int age = 25;
string name = "Alice";
cout << "Name: " << name << ", Age: " << age << endl;
// Without endl (no new line)
cout << "Hello ";
cout << "World!";
cout << endl;
// Using \n instead of endl
cout << "Line 1\nLine 2\nLine 3" << endl;
return 0;
}
Output:
Welcome to C++!
Name: Alice, Age: 25
Hello World!
Line 1
Line 2
Line 3
🔹 Basic Input with cin
The cin object, tied to the standard input stream, reads user-provided data from the keyboard
during program execution. It uses the extraction operator (>>), which automatically
skips leading whitespace and reads data based on the variable's type. However, it stops reading at the next
whitespace character. For robust input, especially when expecting multiple words or handling potential errors, it's
often combined with methods like getline() or checked with conditional statements to verify the input
operation succeeded and clear the input buffer if needed.
#include <iostream>
using namespace std;
int main() {
string name;
int age;
double salary;
// Getting user input
cout << "Enter your name: ";
cin >> name;
cout << "Enter your age: ";
cin >> age;
cout << "Enter your salary: ";
cin >> salary;
// Display the input
cout << "\n--- Your Information ---" << endl;
cout << "Name: " << name << endl;
cout << "Age: " << age << " years" << endl;
cout << "Salary: $" << salary << endl;
return 0;
}
Sample Run:
Enter your name: John
Enter your age: 30
Enter your salary: 50000
--- Your Information ---
Name: John
Age: 30 years
Salary: $50000
🔹 Reading Strings with Spaces
To read entire lines of text including spaces, the getline() function is indispensable, as
cin >> stops at whitespace. The standard syntax
getline(cin, stringVariable) reads from the input stream until it encounters a newline character,
storing the result in a string object. This is crucial for reading full names, addresses, sentences, or any
multi-word user input. It's important to manage the input buffer correctly; sometimes a preceding
cin >> operation leaves a newline in the buffer, which requires using cin.ignore()
before getline() to get the intended input.
#include <iostream>
#include <string>
using namespace std;
int main() {
string fullName;
string address;
cout << "Enter your full name: ";
getline(cin, fullName);
cout << "Enter your address: ";
getline(cin, address);
cout << "\nYour Details:" << endl;
cout << "Full Name: " << fullName << endl;
cout << "Address: " << address << endl;
return 0;
}
Sample Run:
Enter your full name: John Smith
Enter your address: 123 Main Street
Your Details:
Full Name: John Smith
Address: 123 Main Street
🔹 Formatting Output
Advanced output formatting in C++ is controlled using manipulators from the <iomanip> and
<iostream> libraries, allowing precise control over presentation. Common
manipulators include setprecision(n) for decimal places, fixed for fixed-point notation,
setw(n) to set field width, and left/right for alignment. These tools are
essential for creating professional, readable output such as formatted tables, aligned columns of numbers, financial
reports with consistent decimal places, or scientific data displayed with specific precision, ensuring the output is
both user-friendly and publication-ready.
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
double pi = 3.14159265;
int number = 42;
// Set decimal precision
cout << "Pi with 2 decimals: " << fixed << setprecision(2) << pi << endl;
cout << "Pi with 4 decimals: " << fixed << setprecision(4) << pi << endl;
// Set field width
cout << "Number with width 10: " << setw(10) << number << endl;
cout << "Number with width 10 (left): " << left << setw(10) << number << "|" << endl;
// Fill with characters
cout << "Number with zeros: " << setfill('0') << setw(5) << number << endl;
return 0;
}
Output:
Pi with 2 decimals: 3.14
Pi with 4 decimals: 3.1416
Number with width 10: 42
Number with width 10 (left): 42 |
Number with zeros: 00042