0% found this document useful (0 votes)
4 views

Basic Input Output

The document discusses basic input/output in C++. It explains that input and output in C++ is performed via streams and describes common input and output streams like cout, cin, cerr, and clog. It provides examples of using these streams like cout for output, cin for input, cerr for unbuffered errors, and clog for buffered errors.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Basic Input Output

The document discusses basic input/output in C++. It explains that input and output in C++ is performed via streams and describes common input and output streams like cout, cin, cerr, and clog. It provides examples of using these streams like cout for output, cin for input, cerr for unbuffered errors, and clog for buffered errors.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

1/16/24, 12:02 PM Practice | GeeksforGeeks | A computer science portal for geeks

DSA
Tutorials R
Data Science
Web Dev
Basic Input/Output in C++

C++ comes with libraries that provide us many ways for performing input and output.
In C++ input and output is 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 a 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.

Header files available in C++ for Input - Output operation are:


iostream: iostream stands for standard input output stream. This header file
contains definitions to objects like cin, cout, cerr etc.
iomanip: iomanip stands for input output manipulators. The methods declared in
this files are used for manipulating streams. This file contains definitions of setw,
setprecision etc.
fstream: This header file mainly describes the file stream. This header file is used
to handle the data being read from a file as input or data being written into the
file as output.

In C++ articles, these two keywords cout and cin are used very often for taking inputs
and printing outputs. These two are the most basic methods of taking input and output
in C++. For using cin and cout we must include the header file iostream in our program.

In this article, we will mainly discuss the objects defined in the header file iostream like
cin and cout.
Standard output stream (cout): Usually the standard output device is the display
screen. cout is the instance of the ostream class. cout is 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)
We use cookies
Articles Read usingyou
to ensure thehave
insertion
the bestoperator (<<).
 Prev on our website. By using our site, you
browsing experience Got
3 of 24 Complete. (13%) it!
CPP that you have read and understoodNext
acknowledge ourCookie Policy & Privacy Policy

https://www.geeksforgeeks.org/batch/cip-1/track/cip-intro-var-operators/article/MjUyNw%3D%3D 1/4
1/16/24, 12:02 PM Practice | GeeksforGeeks | A computer science portal for geeks
Dash

#include <iostream>
All

using namespace std;

Articles
int main( ) {
char sample[] = "GeeksforGeeks";

Videos
cout << sample << " - A computer science portal for geeks";

return 0;
Problems
}

Output: Quiz

GeeksforGeeks - A computer science portal for geeks

As you can see in the above program the insertion operator(<<) insert the value
of the string variable sample followed by the string "A computer science portal
for geeks" in the standard output stream cout which is then displayed on screen.
standard input stream (cin): Usually the input device is the keyboard. cin is the
instance of the class istream and is used to read input from the standard input
device which is usually the keyboard. The extraction operator(>>) is used along
with the object cin for reading inputs. The extraction operator extracts the data
from the object cin which is entered using the keboard.
CPP

#include<iostream>
using namespace std;

int main()
{
int age;
Menu
cout << "Enter your age:";
cin >> age;
We use cookies to ensure
Articles Read
coutyou
<<have the bestage
"\nYour browsing experience

is: "<<age; on our website. By using our site, you
3 of 24 Complete. (13%)
acknowledge that you have read and understood ourCookie Policy & Privacy Policy

https://www.geeksforgeeks.org/batch/cip-1/track/cip-intro-var-operators/article/MjUyNw%3D%3D 2/4
1/16/24, 12:02 PM Practice | GeeksforGeeks | A computer science portal for geeks
Dash
return 0;
}

All
Output:

Enter your age:


Articles
Input : 18

Your age is: 18


Videos

The above program asks the user to input the age. The object cin is connected to
the input device. The age enteredProblems
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.
Un-buffered standard error streamQuiz (cerr): cerr is the standard error stream
which is used to output the errors. This is also an instance of the ostream class. As
cerr is un-buffered so it is used when we need to display the error message
immediately. It does not have any buffer to store the error message and display
later.
CPP

#include <iostream>

using namespace std;

int main( )
{
cerr << "An error occured";

return 0;
}

Output:
Menu
An error occured

We use cookies to ensure you have the best browsing experience


Articles Read  on our website. By using our site, you
buffered
3 of 24 Complete. (13%)standard error stream (clog): This is also an instance of the ostream
acknowledge that you have read and understood ourCookie Policy & Privacy Policy
class and used to display errors but unlike cerr the error is first inserted into a
https://www.geeksforgeeks.org/batch/cip-1/track/cip-intro-var-operators/article/MjUyNw%3D%3D 3/4
1/16/24, 12:02 PM Practice | GeeksforGeeks | A computer science portal for geeks
Dash
buffer and is stored in the buffer until it is not fully filled. The error message will
be displayed on the screen too.
CPP All

Articles
#include <iostream>

using namespace std;


Videos

int main( )
{
clog << "An error occured";Problems

return 0;
} Quiz

Output:

An error occured

Marked as Read

 Report An Issue

If you are facing any issue on this page. Please let us know.

Menu

We use cookies to ensure you have the best browsing experience


Articles Read  on our website. By using our site, you
3 of 24 Complete. (13%)
acknowledge that you have read and understood ourCookie Policy & Privacy Policy

https://www.geeksforgeeks.org/batch/cip-1/track/cip-intro-var-operators/article/MjUyNw%3D%3D 4/4

You might also like