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

By Prof. Fazal Rehman Shamil
Last modified on August 7th, 2021

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 (<<)

Examples of cout:

Printing “Same to Same” Message

cout<<“C++ Programming”;

This line will display “C++ Programming” on the screen. 

The string constant is always enclosed in double-quotes.

Printing Values without Variable

cout<<245;
This line will display 245 on the screen. 

The numeric constant is not enclosed in quotation marks.

Printing Values with variables:

int price=200; //price is a variable and 200 is saved in price. The price variable is of integer type.

cout<<price;

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

Printing message with variables:

int price=200; //price is a variable and 200 is saved in price. The price variable is of integer type.

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 twice to concatenate the string value with the value of Price.

 

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 or array etc. The variable/array etc are used to store the input data.

Examples:

Example 1:

The above line will get an integer type value from the keyboard and store it in variable y.

Example 2:

The above line will get three integer type values from the keyboard and store them in variables x,y and z.

Prof.Fazal Rehman Shamil (Available for Professional Discussions)
1. Message on Facebook page for discussions,
2. Video lectures on Youtube
3. Email is only for Advertisement/business enquiries.