Basic Input / Output in C++
In C++, input and output are performed in the form of a sequence of bytes or more commonly known as streams.
- Input Stream: If the direction of flow of bytes is from the device (for example, Keyboard) to the main memory then this process is called input.
- Output Stream: If the direction of flow of bytes is opposite, i.e. from main memory to device (display screen) then this process is called output.
All of these streams are defined inside the <iostream> header file which contains all the standard input and output tools of C++. The two instances cout and cin of iostream class are used very often for printing outputs and taking inputs respectively. These two are the most basic methods of taking input and printing output in C++.
Standard Output Stream - cout
The C++ cout is the instance of the ostream class used to produce output on the standard output device which is usually the display screen. The data needed to be displayed on the screen is inserted in the standard output stream (cout) using the insertion operator(<<).
Syntax
cout << value/variable;
For example, if we want to print text "GeeksforGeeks" on the display, we can use the cout as shown:
#include <iostream>
using namespace std;
int main() {
// Printing the given text using cout
cout << "GeeksforGeeks";
return 0;
}
Output
GeeksforGeeks
Explanation: In the above program, cout is used to output the text "GeeksforGeeks" to the standard output stream. It works in conjunction with the insertion operator (<<) to send the specified data to the output stream.
We can also print the variable values using cout.
#include <iostream>
using namespace std;
int main() {
int a = 22;
// Printing variable 'a' using cout
cout << a;
return 0;
}
Output
22
Understanding input and output operations is essential for any C++ programmer. The C++ Course includes comprehensive lessons on basic I/O operations, ensuring you can manage user interaction in your programs.
Standard Input Stream - cin
The C++ cin statement is the instance of the class istream and is used to read input from the standard input device which is usually a keyboard. The extraction operator (>>) is used along with the object cin for extracting the data from the input stream and store it in some variable in the program.
Syntax
cin >> variable;
For example, if we want to ask user for his/her age, then we can use cin as shown:
#include <iostream>
using namespace std;
int main() {
int age;
// Taking input from user and store
// it in variable
cin >> age;
// Output the entered age
cout << "Age entered: " << age;
return 0;
}
Output
18 (Enter by user)
Your age is: 18
Explanation: The above program asks the user to input the age. The object cin is connected to the input device (keyboard). The age entered by the user is extracted from cin using the extraction operator(>>) and the extracted data is then stored in the variable age present on the right side of the extraction operator.
The type of input provided should be same as that of the variable being used to store it. Otherwise, it may lead to undefined error or input failure.
Also, while taking text as input using cin, we need to remember that cin stops reading input as soon as it encounters a whitespace (space, tab, or newline). This means it only captures the first word or characters until the first whitespace. It is shown in the below example:
#include <iostream>
using namespace std;
int main() {
string name;
// Taking input from user and store
// it in variable
cin >> name;
// Output the entered age
cout << "Name entered: " << age;
return 0;
}
Output
Vishal Kumar (Enter by user)
Name entered: Vishal
Un-buffered Standard Error Stream - cerr
The C++ cerr is the standard error stream that is used to output the errors. This is also an instance of the iostream class. As cerr in C++ is un-buffered so it is used when one needs to display the error message immediately. It does not have any buffer to store the error message and display it later.
The main difference between cerr and cout comes when you would like to redirect output using "cout" that gets redirected to file if you use "cerr" the error doesn't get stored in file.(This is what un-buffered means ..It cant store the message)
Example:
#include <iostream>
using namespace std;
int main() {
cerr << "An error occurred";
return 0;
}
Error
An error occurred
Buffered Standard Error Stream - clog
This is also an instance of ostream class and used to display errors but unlike cerr the error is first inserted into a buffer and is stored in the buffer until it is not fully filled. or the buffer is not explicitly flushed (using flush()). The error message will be displayed on the screen too.
#include <iostream>
using namespace std;
int main() {
clog << "An error occurred";
return 0;
}
Error
An error occurred
If you do not understand the difference between cerr and clog, refer to this article - Difference between cerr and clog