In this tutorial, you will learn how to take input from the user and display output on the screen through cin
and cout
in C++. In order to understand this tutorial, you must have a good grip on the concepts of data types and variables, streams, and also using namespace std
.
Table of Contents
cin
is a keyword and also an object of the output stream that reads the data from the standard input device i.e. the keyboard. Then it inserts that data into the variables in your program. Similarly, cout
is also a keyword and the object of the standard output stream. It fetches the values from the variables and displays them to the standard output device i-e the monitor. The visual below visualizes the concept.
Display Output Using cout
in C++
We can understand cout
with a simple real-life scenario. Take a teacher who is speaking in a classroom with students. The students here are the user and the teacher here is the program. The teacher speaking is representing the job of cout
by providing information to the students (user).
In C++ cout is a pre-defined object of the output stream that we have explained above. You will use this to display information on the screen by sending the data from your program to the monitor. Cout works with the insertion operator << that inserts the data into the stream.
Now let’s implement a program to use cout
.
Display a Message to the User Using cout
The phrase “Welcome to the program!” is inserted into the output stream using the <<
insertion operator. This instructs the program to display that message on the console. In C++, endl
(short for “end line”) is a keyword used with cout
to insert a newline character into the output stream.
Example Code
#include <iostream> using namespace std; int main() { cout << "Welcome to the program!" << endl; return 0; }
Output
Welcome to the program!
Displaying a Single Variable to the User Using cout
You can also use cout
to display data stored in the variables, all different types of data types can be displayed in the same way using the following syntax for cout
.
You can display a variable in between a statement that is to be printed where the age is being displayed in the following example code.
Example Code
#include <iostream> using namespace std; int main() { int age = 25; string name = "john"; char gender = 'M'; float height = 5.9; cout << "Welcome to the program!" << endl; cout << "Your name is: " << name << endl; cout << "Your age is: " << age << " years" << endl; cout << "Your gender is: " << gender << endl; cout << "Your height is: " << height << endl; return 0; }
Output
Welcome to the program! Your name is: john Your age is: 25 years Your gender is: M Your height is: 5.9
Display Multiple Variables in a Single Line
You can also display multiple variables to the user in a single line of code using a single cout
keyword with multiple stream insertion operators <<
followed by the text or variables. The text and variables are combined in the output stream to produce the final output.
Example Code
#include <iostream> using namespace std; int main() { int num1 = 5, num2 =10, num3 = 15; cout << "Num1: " << num1 << ", Num2: " << num2 << ", Num3: " << num3 << endl; return 0; }
Output
Num1: 5 Num2: 10 Num3: 15
Input Values from User Using cin
cin
is also a pre-defined object of the input stream. A simple way to understand cin is with the same example we discussed.
Think of it as water being stored in a container through an input stream from a source, or in other words, data flowing into the program from the user through the keyboard.
Another simple example to grasp the concepts of cin
is a teacher standing in the classroom listening to the students concerns so here the teacher is working like the cin
and the students speaking are the cout
in this scenario.
Similar to cout
, cin
works with extraction operator i-e >>
that extracts the data from the stream to the variable.
Input a Single Value from the User Using cin
In this example, we use cin
to read the user’s name and age. The user is prompted to enter their name, and the input is stored in the variable name. Similarly, the user is prompted to enter their age, and the input is stored in the variable age through cin
. Finally, the program displays a message along with the age.
Example Code
#include <iostream> using namespace std; int main() { int age; cout << "Please enter your age: "; cin >> age; cout << "Your age is: " << age << endl; return 0; }
Output
Please enter your age:25 Your age is: 25
Input Multiple Values from User in a Single Line
You can also input multiple values from the user using a single line of code using a single cin with multiple stream extraction operators >>. When you use multiple extraction operators in a single statement, cin
reads and stores each input sequentially.
Each >>
operator extracts the next piece of input from the input stream. When cin >> num1
is executed, cin
reads input until it encounters whitespace (space, tab, or newline).
In the following code, the user types three numbers separated by spaces and presses Enter. The first number is read and stored in num1
.The second number is read and stored in num2
.The third number is read and stored in num3
.
Example Code
#include <iostream> using namespace std; int main() { int num1, num2, num3; cout << "Please enter the numbers: "; cin >> num1 >> num2 >> num3; cout << "Num1: " << num1 << endl; cout << "Num2: " << num2 << endl; cout << "Num3: " << num3 << endl; return 0; }
Output
Please enter the numbers: 5 10 15 Num1: 5 Num2: 10 Num3: 15
Conclusion
To sum up, input and output operations are important in C++ programming. They involve using stream objects like cin (for input) and cout (for output) to interact with the user. With cin, you can take input from the user, while cout allows you to display messages and variables. By understanding these concepts, you can create interactive programs that take user Input, perform calculations, and provide output. It’s a fundamental aspect of building user-friendly applications and handling data effectively. Now take our quiz on input and output in C++ to make sure you’ve understood the concepts.
Pingback: Standard Input/Output Stream in C/C++ - Syntax Scenarios