How to input/output in C++ (CPP, C Plus Plus)

How to input/output in C++ (CPP, C Plus Plus)

When you type at the keyboard and your program takes your input as data that is standard input. When your output is displayed at the terminal screen that is standard output. The standard input stream is called cin, The standard output stream is called cout.

To get basic I/O functionality, you must #include < iostream > library in header of C++ program. 

Standard Output

By default. the term standard output refers to the output display on the screen. C++ language uses the cout stream object to display standard display. The word ‘cout‘ stands for console output. The cout object is used with the insertion operator (<<)

Syntax:

cout<<Variable/Constant/Expression;

Examples:

cout<<“C++ Programming”;

The above line will display “C++ Programming” on the screen. The string constant is always enclosed in double quotes.

cout<<245;

The above line will display 245 on the screen. The numeric constant is not enclosed in quotation marks.

cout<<price;

The above line will display
the value of variable
a on the screen. The variable is not enclosed in quotation  marks;

cout<<“The Value of Price”<<price;

The above line will displays “The Value of Price” along with the value of variable Price on the screen. The insertion operator 

is used for twice to concatenate the string value with the value of Price.

C++ Output simple Example

// header file

#include <iostream>  
// namespace
using namespace std;   
//main function
int main() 
{
// initialization variable price
int price=1000;  
// string output
cout<<“Output Example”; 
// new line
cout<<endl; 
// output constant variable value price
cout<<price; 
// new line
cout<<endl; 
//string with constant variable value price
cout<<“The value of price =”<<price<<“$”; 
// return int main function
return 0;
}

Output

Standard Input

 By default, the term standard input refers to the input given vie keyboard. C++ language uses cin stream object to get standard input. The word ‘cin‘ stands for console input. C++ handles standard input by applying the extraction operator (>>) with cinstream.The operator must be followed by a variable. The variable is used to store the input data.

Syntax:

cin>>variable;

Examples:

cin>>y;
The above line will get a value from the keyboard and store it in variable y.

cin>>a>>b>>c;

The above line will get three values from keyboard and store in variables a,b and c.

C++ Input simple Example

// header file
#include <iostream>  
// namespace
using namespace std;   
//main function
int main() 
{
// declare variable price
int price; 
// output string
cout<<“Input Example”;
// new line
cout<<endl;
// output string
cout<<“Enter the value of Price:”;
// input for price variable 
cin>>price;
// new line
cout<<endl;
// output string with store value of price
cout<<“The value of price =”<<price<<“$”;
// return main function
return 0;

}