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:
Stream | Description |
cin | standard input stream |
cout | standard output stream |
cerr | standard error (output) stream |
clog | standard 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<< ;
In C++ if we want to show a variable x from to user on screen we will write.
int x=10;
cout << x;
E.g.
#include "stdafx.h"
#include"iostream"
using namespace std;
void main(){
int x=10;
cout << “Value of X is :” << x << endl;
}
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: –
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>>;
In C++ if we want to take input to a variable x from the user using keyboard we will write.
int x;
cin>>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: –
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: –
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.
Awesome work for Students.
When will you make java tutorials?
is images can be added in C++ programs
Yes, you can work with images in C++.