Input and Output in C++
Imagine you’re creating a simple program that asks for a student’s name and then displays a welcome message. For this to happen, the program first needs to take input from the user and then show the output on the screen. This is known as input and output.
In C++, input and output are handled using two built-in objects: cin and cout. These are available through the iostream header file and are used in almost every C++ program you write.
Once you understand how to use cin and cout, you’ll be able to create interactive programs that can accept user data and display meaningful results.
Displaying Output Using cout
The cout object is used to display information on the screen. It can print text, numbers, variables, or even the result of calculations.
Example
#include <iostream>
using namespace std;
int main() {
cout << "Welcome to CoderMantra!";
return 0;
}
Output
Welcome to CoderMantra!
In this example, the message inside the double quotes is displayed on the screen. The << operator is called the insertion operator, and it sends data to the output stream.
You can also display the value stored in a variable.
Example
#include <iostream>
using namespace std;
int main() {
int marks = 95;
cout << "Marks: " << marks;
return 0;
}
Output
Marks: 95
Here, the text and the variable are displayed together using the << operator.
Taking Input Using cin
While cout displays information, cin is used to receive input from the user. Whatever the user enters is stored inside a variable.
Example
#include <iostream>
using namespace std;
int main() {
int age;
cout << "Enter your age: ";
cin >> age;
cout << "Your age is " << age;
return 0;
}
Sample Output
Enter your age: 20
Your age is 20
In this program, the user enters a value using the keyboard. The >> operator is called the extraction operator, and it stores the entered value in the variable.
Using cin and cout Together
Most C++ programs use both input and output together. The program first accepts information from the user and then displays the result.
C++ cin cout Example
#include <iostream>
using namespace std;
int main() {
string name;
cout << "Enter your name: ";
cin >> name;
cout << "Welcome, " << name << "!";
return 0;
}
Sample Output
Enter your name: Rahul
Welcome, Rahul!
This simple program takes the user’s name as input and displays a personalized welcome message.
đź’ˇ Quick Tip
The
cinobject reads input only until it encounters a space. So, if you enter Rahul Sharma, only Rahul will be stored. You’ll learn how to read complete lines usinggetline()in a later tutorial.
Key Points
- Input and Output in C++ are performed using
cinandcout. coutis used to display information on the screen.cinis used to accept input from the user.- The
<<operator is used withcout, while the>>operator is used withcin. - Learning
cinandcoutis essential because they are used in almost every C++ program.
Frequently Asked Questions (FAQs)
Q1. What is input and output in C++?
Input and output in C++ refer to the process of receiving data from the user and displaying results on the screen. The cin object is used for input, while cout is used for output.
Q2. What is the difference between cin and cout in C++?
cin is used to read input from the user through the keyboard, whereas cout is used to display text, numbers, and other output on the screen.
Q3. Why is the iostream header file required for input and output in C++?
The iostream header file provides the definitions for standard input and output objects like cin, cout, cerr, and clog, making them available in a C++ program.
Q4. Can cin accept different data types in C++?
Yes. cin can read various data types such as integers, floating-point numbers, characters, strings, and more, depending on the type of the variable receiving the input.
Q5. What are the >> and << operators in C++?
The >> operator is the extraction operator used with cin to receive input, while the << operator is the insertion operator used with cout to display output.
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.
