Input and output are used to communicate with the user. In C++, the standard input and output streams are commonly handled using std::cin, std::cout, and std::cerr.
Input means receiving data from the user or another input source. For example, a program can ask the user to enter their age.
int age;
std::cin >> age;
Here, the value entered by the user is stored in age.
Output means displaying information produced by a program.
std::cout << "Hello World";
This statement displays text on the standard output stream.
The <iostream> header provides facilities for standard input and output.
#include <iostream>
It is commonly included when using std::cin and std::cout.
std::cout is used to send output to the standard output stream.
#include <iostream>
int main() {
std::cout << "Welcome to C++";
return 0;
}
Text can be displayed by placing it inside double quotation marks.
std::cout << "Hello Students";
The text inside the quotation marks is displayed as output.
Variables can also be displayed using std::cout.
int age = 20;
std::cout << age;
The value stored in age is displayed.
The << operator is used with std::cout to send data to the output stream.
std::cout << "Age: " << age;
Multiple pieces of data can be sent to the output stream in one statement.
int age = 20;
double marks = 85.5;
std::cout << "Age: " << age;
std::cout << " Marks: " << marks;
The insertion operator can be chained to display multiple values.
std::endl inserts a newline and flushes the output stream.
std::cout << "Hello" << std::endl;
std::cout << "Welcome";
The newline escape sequence \n can also be used to move output to the next line.
std::cout << "Hello\n";
std::cout << "Welcome";
Unlike std::endl, writing \n does not itself request a flush.
std::cin is commonly used to read formatted input from the standard input stream.
int age;
std::cin >> age;
The entered value is stored in age.
The >> operator is used with std::cin to extract formatted input.
int number;
std::cin >> number;
The input is converted to the type of the destination variable when the formatted extraction succeeds.
#include <iostream>
int main() {
int age;
std::cout << "Enter your age: ";
std::cin >> age;
std::cout << "Your age is " << age;
return 0;
}
This program reads an integer and then displays it.
Multiple variables can be read using multiple extraction operations.
int age;
double marks;
std::cin >> age;
std::cin >> marks;
The user can enter the values separately or, for formatted extraction, provide them separated by whitespace.
int age;
double marks;
std::cin >> age >> marks;
The extraction operator can be chained to read multiple values.
char grade;
std::cout << "Enter grade: ";
std::cin >> grade;
std::cout << "Grade: " << grade;
The character entered by the user is stored in grade.
For a std::string, std::cin normally reads one whitespace-delimited word.
#include <iostream>
#include <string>
int main() {
std::string name;
std::cout << "Enter your name: ";
std::cin >> name;
std::cout << "Hello " << name;
return 0;
}
Use std::getline() when you want to read an entire line, including spaces.
#include <iostream>
#include <string>
int main() {
std::string fullName;
std::cout << "Enter your full name: ";
std::getline(std::cin, fullName);
std::cout << "Name: " << fullName;
return 0;
}
When std::getline() is used after formatted extraction such as std::cin >> age, a leftover newline may be read immediately.
int age;
std::string name;
std::cin >> age;
std::getline(std::cin >> std::ws, name);
Using std::ws here consumes leading whitespace before reading the line.
Escape sequences are special character combinations used inside string or character literals.
std::cout << "Hello\nWorld";
std::cout << "Name:\tRahul";
Common examples include \n for a newline and \t for a horizontal tab.
Boolean values can be printed directly.
bool passed = true;
std::cout << passed;
By default, a Boolean value is typically displayed as 1 for true and 0 for false.
The std::boolalpha stream manipulator can be used to display Boolean values as true and false.
#include <iostream>
int main() {
bool passed = true;
std::cout << std::boolalpha << passed;
return 0;
}
std::cerr is the standard error stream and is commonly used for error messages.
#include <iostream>
int main() {
std::cerr << "An error occurred.";
return 0;
}
std::clog is the standard logging stream. It can be used for diagnostic or logging messages.
#include <iostream>
int main() {
std::clog << "Program started.";
return 0;
}
#include <iostream>
int main() {
std::string name;
int age;
std::cout << "Enter your name: ";
std::cin >> name;
std::cout << "Enter your age: ";
std::cin >> age;
std::cout << "Name: " << name << std::endl;
std::cout << "Age: " << age << std::endl;
return 0;
}
Formatted input can fail if the entered data cannot be converted to the requested type.
int age;
std::cin >> age;
If the user enters invalid input such as non-numeric text when an integer is expected, the stream can enter a failure state.
The state of an input stream can be checked using it in a condition.
int age;
if (std::cin >> age) {
std::cout << "Valid input";
} else {
std::cout << "Invalid input";
}
This allows a program to respond when input extraction fails.
#include <iostream>
#include <string>
int main() {
std::string name;
int age;
double marks;
std::cout << "Enter your name: ";
std::cin >> name;
std::cout << "Enter your age: ";
std::cin >> age;
std::cout << "Enter your marks: ";
std::cin >> marks;
std::cout << "\n--- Student Details ---\n";
std::cout << "Name: " << name << std::endl;
std::cout << "Age: " << age << std::endl;
std::cout << "Marks: " << marks << std::endl;
return 0;
}
| Stream / Feature | Purpose | Example |
|---|---|---|
| std::cout | Standard output | std::cout << "Hello"; |
| std::cin | Formatted standard input | std::cin >> age; |
| std::cerr | Standard error output | std::cerr << "Error"; |
| std::clog | Standard logging output | std::clog << "Log"; |
| std::getline() | Read an entire line | std::getline(std::cin, name); |
| std::endl | Insert newline and flush | std::cout << std::endl; |
| \n | Insert a newline | std::cout << "\n"; |
Question: Which object is commonly used to take formatted input from the user in C++?