C++ Input and Output
In every programming language, a program needs a way to communicate with the user. Sometimes it asks the user to enter data, and sometimes it displays the result on the screen. This process is called C++ Input and Output.
In C++, input is mainly taken using cin, while output is displayed using cout. The language also provides features like endl, escape sequences, and formatting functions to make the output more readable.
Let’s understand each of them one by one.
C++ cout
The cout object is used to display output on the screen. It belongs to the iostream library and sends data to the console.
The insertion operator (<<) is used with cout to print text or values.
Syntax
cout << value;
Example
#include <iostream>
using namespace std;
int main()
{
cout << "Welcome to CoderMantra";
return 0;
}
Output
Welcome to CoderMantra
You can also print variables using cout.
int age = 20;
cout << age;
Here, the value stored in the variable age is displayed on the screen.
C++ cin
The cin object is used to accept input from the keyboard. It also belongs to the iostream library.
The extraction operator (>>) is used with cin to store the value entered by the user into a variable.
Syntax
cin >> variableName;
Example
#include <iostream>
using namespace std;
int main()
{
int age;
cout << "Enter your age: ";
cin >> age;
cout << "Your age is " << age;
return 0;
}
Output
Enter your age: 21
Your age is 21
Here, cin reads the value entered by the user and stores it in the variable age.
endl
Sometimes you want the next output to appear on a new line. In C++, this is done using endl.
Example
cout << "Hello";
cout << endl;
cout << "World";
Output
Hello
World
You can also use endl like this:
cout << "Hello" << endl;
cout << "World";
Both methods produce the same output.
Note: endl not only moves the cursor to the next line but also clears the output buffer.
C++ Escape Sequences
C++ Escape Sequences are special characters that begin with a backslash (\). They are used to perform special actions like moving to a new line or adding a tab space.
Some commonly used escape sequences are:
| Escape Sequence | Description |
|---|---|
\n | New line |
\t | Horizontal tab |
\" | Double quotation mark |
\\ | Backslash |
\' | Single quotation mark |
Example
cout << "Name\tAge";
cout << "\nRahul\t20";
Output
Name Age
Rahul 20
Escape sequences make the output more neat and readable.
C++ Output Formatting
Sometimes the default output is not enough. You may want to display numbers with a fixed number of decimal places or align text properly. This is called C++ Output Formatting.
For these formatting functions, include the following header file:
#include <iomanip>
setw()
The setw() function sets the width of the output field. It is useful for displaying data in columns.
Example
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
cout << setw(10) << "Apple";
cout << setw(10) << "Mango";
return 0;
}
Output
Apple Mango
This makes the output look more organized.
setprecision()
The setprecision() function controls how many digits are displayed for floating-point numbers.
Example
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
double num = 12.345678;
cout << setprecision(4) << num;
return 0;
}
Output
12.35
The number is rounded according to the precision you specify.
fixed()
The fixed manipulator is used with setprecision() to display a fixed number of digits after the decimal point.
Example
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
double price = 199.5;
cout << fixed << setprecision(2) << price;
return 0;
}
Output
199.50
Without fixed, setprecision() counts the total number of significant digits. With fixed, it counts only the digits after the decimal point.
Frequently Asked Questions (FAQs)
Q1. What is the difference between cout and cin in C++?
C++ cout is used to display output on the screen, while C++ cin is used to accept input from the user. Together, they form the foundation of C++ Input and Output operations.
Q2. What is the purpose of endl in C++?
The endl manipulator moves the cursor to the next line and flushes the output buffer. It is commonly used with C++ cout to improve the readability of program output.
Q3. What are escape sequences in C++?
C++ Escape Sequences are special character combinations that begin with a backslash (\). They are used to insert characters such as a new line (\n), tab (\t), double quote (\"), and backslash (\\) into the output.
Q4. How can I format output in C++?
C++ Output Formatting can be done using manipulators like setw(), setprecision(), and fixed(). These help control field width, decimal precision, and the display format of floating-point values.
Q5. What is the difference between fixed() and setprecision() in C++?
setprecision() specifies the number of digits to display, while fixed() makes setprecision() control the number of digits after the decimal point. They are often used together for accurate C++ Output Formatting.
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.
