Home > C++ > C++ Input Output

In C++ Input output is done using the standard library, which provides a convenient mechanism to interact with users using its input and output features.
In this section, we will introduce you that how input-output is done in c++.
C++ uses streams to perform input and output operations in sequential media such as Console screen, keyboard or files. Streams can be defined as entities from where a program can extract characters or can insert characters into it. In general, we can say that streams are source and destination of characters which are used sequentially.
Different streams available In C++ standard library are:

StreamDescription
cinstandard input stream
coutstandard output stream
cerrstandard error (output) stream
clogstandard logging (output) stream

Standard Output cout: –

In most of the program environments standard output is screen and c++ uses cout from the standard library to give output to console. cout is used with insertion operators (i.e. two less than signs <<).
Its basic syntax is:
     cout&lt;&lt; ;
In C++ if we want to show a variable x from to user on screen we will write.
int x=10;
     cout &lt;&lt; x;
E.g.

#include "stdafx.h"
#include"iostream"

using namespace std;

void main(){
int x=10;
cout << “Value of X is :” << x << endl;
}

Output: –
c++ input output
Anything written inside double quotes i.e. inverted commas will be printed as it while values will be printed for variables or expressions written without double quotes.
More than one cout statements can be combined as shown below.

#include "stdafx.h"
#include"iostream"

using namespace std;

int main()
{
int x=10;

int y=20;

int z=30;
cout << “Value of X is :” << x << endl;
cout << “Value of Y is :” << y << endl;
cout << “Value of Z is :” << z << endl;
return 0;

}

This above code can also be written as:

#include "stdafx.h"
#include"iostream"

using namespace std;

int main()
{
int x=10, y=20, z=30;
cout << “Value of X is :” << x << endl << “Value of Y is :” << y << endl <<
“Value of Z is :” << z << endl;
return 0;
}

Above written both will give us same output.
output: –
cout

Standard Input cin: –

In most of the programs, the default input standard is keyboard and cin is the standard input stream is used to take input from the keyboard.
cin is used with extraction operator (i.e. two greater than signs >>).
Its basic syntax is:
     cin&gt;&gt;;
In C++ if we want to take input to a variable x from the user using keyboard we will write.
int x;
     cin&gt;&gt;x;
E.g.

#include "stdafx.h"
#include"iostream"

using namespace std;

int main()
{
int x;
cout << “Please Enter Value : ” << endl; cin >> x;
return 0;
}
output: –
Get input from user in c++

c++ input output

We can take input in the number of variables using a single cin statement.

#include "stdafx.h"
#include"iostream"

using namespace std;

int main()
{
int x,y,z;
cout << “Please Enter Values for X, Y & Z : ” << endl; cin >> x >> y >> z;
cout << “Value of X is :” << x << endl;
cout << “Value of Y is :” << y << endl;
cout << “Value of Z is :” << z << endl;
return 0;
}
output: –
c++ input output

cerr & clog: –
cerr & clog are also output streams, and they also work like cout, with the only difference being that they identify streams for specific purposes:
cerr identifies error messages.
clog identifies logging.

4 thoughts on “C++ Input Output

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.