C++ Input Output

In this tutorial, you will learn how to use cin object to take input from the user and cout object to display output to the user with the help of examples.


In your program, you might want to take a user input in one part, and to do that you would need to use an operator of C++ known as cin>>. Almost all of the input from the input device like the keyboard is taken through this and then you can store it in a variable and use it in a program. For example, if you type the following lines of code.

I/O Library Header Files

Some of the most common header files used are :

  • <iostream> – It is mostly used to define objects like cin, cout and cerr. They are the standard input stream, standard output stream, and standard error stream respectively.
  • <iomanip> – It is used to declare services that are useful for performing formatted input/output operations like setprecision and setw.
  • <fstream> – It is used to manipulate the services used for user-controlled file processing.

C++ Output Operations

If you want to display something on the output screen, which is usually a monitor, you use the output operations via the C++ operator cout<< which allows you to output any data to the output screen. You can also use endl command to format your output into different lines as shown by the code below.

The cout is a predefined object of the ostream class. The class is associated with a standard output device which is generally a display screen. It is followed by a stream insertion operator or << so that the output is displayed on the screen.

Let’s check a C++ program that will print something to the output screen.

#include <iostream>

using namespace std;

int main( ) 
{

cout << "Hello world on the first line."; //cout is the operator through which we display the output               
cout << "This text will still be on the first line."<<endl;
cout << "This text will be on the second line.";

}

Output

Value of arr is: C++ tutorial on Input and Output Code

C++ Input Operations

In your program, you might want to take a user input in one part, and to do that you would need to use an operator of C++ known as cin>>. Almost all of the input from the input device like the keyboard is taken through this and then you can store it in a variable and use it in a program.

The cin is a predefined object of istream class. The class is associated with a standard input device, which is usually a keyboard. It is followed by a stream extraction operator (>>) to read the input from a console.

Let’s check an example of C++ input operation.

#include <iostream>

using namespace std;

int main( ) 
{ 
int number; // declaring a variable age
cout << "Enter any number :";
cin >> number;
cout<< "You entered the number: " <<number;
}

Output

Enter any number: 24
You entered the number: 24
Here using cin object you can take the name of the user, age, or any specific data which is required by your program as user input, and your code execution will halt and wait for the user to input the data. If the user enters incorrect data, then that might lead to an exception and we will discuss exception handling later and not now.

Standard end line (endl)

The endl is another predefined object of ostream class. It is used to insert any new line characters and flushes the whole stream.

#include <iostream>

using namespace std;

int main( ) 
{
cout << "C++ Tutorial"; 
cout << " Dummy Line"<<endl; 
cout << "End of line"<<endl; 
}

Output

C++ Dummy Line
End of line

This is how C++ treats the endl command and with this, you can appropriately format your output to have greater control over the final result.

Please get connected & share!

Advertisement