Cin and Cout in C+ with example
C++ utilizes the concept of streams to perform input and output operations. A stream is a sequence of bytes in which character sequences are flown into or flowed out of. Streams operate as intermediates between the program and the I/O devices, helping programmers achieve device-independent I/O operations. The two types of streams in C++ are:
- Input stream
- Output stream
Header Files for Input/Output Operations:
<iostream>
: Stands for standard input-output stream. This header file contains definitions for objects likecin
,cout
,cerr
, etc.<iomanip>
: Stands for input-output manipulators. The methods declared in this file are used for manipulating streams and contain definitions of functions likesetw
,setprecision
, etc.<fstream>
: Describes the file stream and is used to handle data being read from a file as input or being written into a file as output.
The objects discussed in this article are cin
and cout
, which are the most commonly used objects for taking inputs and printing outputs.
Standard Output Stream (cout
)
cout
is an object of the ostream
class. The standard output device is the display screen. Characters are inserted into the output stream using the insertion operator (<<
). Multiple insertion operations can be chained in a single statement.
#include <iostream>
using namespace std;
int main() {
int Hello = 10;
cout << "Hello"; // Prints the text "Hello" on the display screen.
cout << Hello; // Prints the contents of the variable 'Hello' (10) on the screen.
cout << 10; // Prints number 10 on the screen.
// Chaining multiple insertions
cout << "Welcome" << " to" << " OpenGenus!";
return 0;
}
Chaining multiple insertions is useful to mix literals and variables in a single statement.
cout << "My name is " << name << " and I am " << age << " years old";
To add a line break, the newline character ('\n'
) or the endl
manipulator can be used.
cout << "This is sentence 1.\n";
cout << "This is sentence 2.";
Formatting the output is done through the use of I/O manipulators. Examples include:
setw()
: Sets the width of the field assigned for the output.setprecision()
: Sets the total number of digits to be displayed when floating-point numbers are printed.
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
cout << setw(6) << "X"; // Output: _ _ _ _ _ X
// Using setprecision
cout << setprecision(5) << 22.0 / 7; // Output: 3.1415
return 0;
}
Standard Input Stream (cin
)
cin
is an object of the istream
class. The standard input device is the keyboard. The extraction operator (>>
) is used with cin
to extract values from the stream.
#include <iostream>
using namespace std;
int main() {
int x, y;
cout << "Enter two integers separated by space: ";
cin >> x >> y;
cout << "Sum: " << x + y;
return 0;
}
Using cin
with strings requires special attention due to whitespace being considered as a terminating character. The getline()
function is often used to read complete lines.
#include <iostream>
using namespace std;
int main() {
string myName;
cout << "Enter your name: ";
getline(cin, myName);
cout << "Welcome " << myName;
return 0;
}
Another way to use getline
with cin
is:
char address[20];
cout << "Address: ";
cin.getline(address, 20);
cout << "You live in " << address;
In summary, cin
and cout
are essential for input and output operations in C++, providing a flexible and powerful way to interact with the user and display information.