C++ Stream: Basic Input/output Stream objects in C++

c++ Stream objects

Table of Contents

Description:

C++ Stream Objects- In this article you will learn how to use cin stream objects and cout stream objects in C++ programming.

The Predefined C++ Stream Object:

Predefined C++ stream objects are used for basic input and output. A C++ stream refers to a flow of data. The data may flow from input device into main memory or from main memory to output device.

C++ provides various predefined C++ stream  objects. The basic input/output C++ stream objects are cin, cout, cerr, and clog. The cin object is associated with standard input and cout object is associated with standard output. The cerr and clog both objects are linked to the standard output.

C++ input/output stream are declared in the header file “iostream.h”. in most of the c++ program, this header file( iostream.h) must be include for the input and output of data.



The ‘cin’ C++ Stream Object:

the word cin stands for console input. It is pronounced as “see in”. it is the standard input C++ stream  object. It is used as input statement to get input from user through keyword during execution of program. This C++ stream object represents the flow of data from input device ( i.e. keyword) to the computer memory. The cin stream input object is also the part of iostream.h header file.

c++ Stream objects

When an input statement is executed, the computer waits to get an input from keyboard. When a value is typed and enter key is pressed, the value is assigned to the variable and control shifts to the next statement.

The syntax of cin is as follow:

Cin>>var1 [>>var2] [>>var3]……….;

Where

cin:

It is the name of standard input C++ stream object.

>>:

It is known as extraction operator or get from operator. It takes input form cin object and assigns it to the variable. We can use multiple extraction operators to get input into multiple variables.

var1,var2,var3……:

it indicates a list of variables separated by extraction operator (>>). There must be a variable on the right-side of each ‘>>’ operator in which the input value is stored.

Examples:

  • Following statement inputs a value from keyboard and stores it in variable marks during the execution of program:

cin>>marks;

  • Following statement gets three values and stores these values in variables x,y and z respectively:

cin>>x>>y>>z;

When this statement is executed, user can enter the values by using one of the following ways:

  • Type value for the first variable and press enter key. The computer will wait to get the value for second variable. Repeat this process until values in all variable have been entered.

OR

  • Type the values of all variables separated by spaces and then press enter key.


cin C++ Stream object Programming Examples:

Write a program that inputs a character and displays its ASCII code using cin cout stream objects:

Output:

Write a program that inputs time in seconds and converts it into standard format (such as hh:mm:ss format) using cin cout stream objects:

Output:


A car can travel 5.3 miles in one liter. Write a program that inputs petrol in liters and displays how much distance the car can cover using the available petrol using cin cout stream objects:

Output:

Example write a program that inputs the name, age, height and gender of a student using ‘cin C++ stream  object’:

Flow chart:

c++ Stream objects

Program:

c++ Stream objects



Example write a program that inputs distance in miles from user and converts miles into kilometers. One mile =1.609 kilometer using cin cout stream objects:

c++ Stream objects

Example write a program in C++ to converts pound into kilograms using cin cout stream objects:

1 pound = 0.453592

c++ Stream objects

Example write a program in C++ which convert Inches to centimeters using cin cout stream objects:

1 inch = 2.54 centimeters

c++ Stream objects


Example writes a program in C++ to calculate the volume and surface area of a sphere using cin cout stream objects:

Formula:

area of sphere = 4π R2
circumference f sphere = 4/3 πR3
the value of π is = 3.1417

Example write a program in C++ which calculate the area and circumference of the circle using cin cout stream objects:

Formula:

area of circle = π R2
circumference of circle= 2πR
the value of π is = 3.1417

Flowchart:

c++ Stream objects

c++ Stream objects

Example write a temperature program in C++ which converts the Fahrenheit to Centigrade using cin cout stream objects:

C=5/9(F-32)

Flowchart:

c++ Stream objects

c++ Stream objects


Example write a program that inputs marks obtained by a student in five different subject and find out aggregate marks (total marks) and percentage using cin cout stream objects:

c++ Stream objects

Example write a program that inputs the value of three sides of a triangle and finds out its area using cin cout stream objects:

Formula:

c++ Stream objects

c++ Stream objects

Example write a program which consists of three variable Area, Perpendicular, and Base. The program inputs the values in variables per and base from the user and computes the area of a triangle using cin cout stream objects:

Formula:

Area = (base*per)/2

c++ Stream objects


Example write a program in C++ which reverse the order of the long numbers using cin cout stream objects:

c++ Stream objects

Write a program that inputs the current price of gas and electricity per unit. Give each item’s rate with an  increment of 20%. The program computes the new price of gas and electricity using cin cout stream objects:

c++ Stream objects

write a program which calculate the employee dearness allowance, medical allowance, house rent allowance and calculate the gross salary and display using cin cout stream objects:

25% dearness allowance.

15% medical allowance.

45% house rent allowance.

formula:

gross salary= basic pay + dearness allowance + medical allowance + house rent.

c++ Stream objects



The ‘cout’  C++ Stream Object:

The cout is pronounced as See Out. The cout stands for console output. The cout is a c++ standard output C++ stream object. It is used as an output statement to display.

Output on the computer screen. This C++ stream object represents the flow of data from computer memory to display screen. It is a part of iostream.h header file.

c++ Stream objects

The  general syntax of cout is as follows:

cout<<const1/var1/exp1[<<consta2/var2/exp2/….];

where

cout:

it is the name of output C++ stream object, which is used to display the standard output.

<<:

It is known as insertion operator or put to operator. It sends the output (contents of variable or constant) to the cout object. The cout object then sends the output to the display screen. We can use multiple insertion operators to display the valued of multiple variables or constants.

Const1/var1/exp1, Const2/var2/exp2:

It indicates the list of constants, variables or arithmetic. Expression. for each constants, or variable or expression. separated insertion operator (<<) is used. The string constant is given in double quotation marks. Character constant is given in singe quotation marks. However, variables, numeric constants and expression are given without quotation marks.

Examples:

  • Following statement will display a message “Programming Digest”:

Cout<<”Programming Digest”;

This output statement consists of only one string constant which is to be displayed on the screen. This string constant is given in double quotation marks.

  • Following statement will display the value of variable ‘sum’ on the screen.

cout<<sum;

Please note that in this output statement the variable ‘sum’ is not enclosed with any quotation marks.

  • Following statement uses one variable “km”, two string constants, one arithmetic expression and four insertion operators:

cout<<km<<” kilometers= “<< km*1000<<”meter”;

Suppose the value of km is 6. The output of the above output statement will be:

3 kilometers= 3000

cout Programming Examples:

Example write a program that computes and display the area of a square. The formula to compute the area of square is using cout stream object:

Area = height * width;

Flowchart:

c++ Stream objects

 

c++ Stream objects

It the above program “Area of square =” is a text message. The text message will be displayed first and the value of variable ‘area’ will be displayed.


Example write a program that adds two floating point values and displays the sum on the screen using cout stream object:

c++ Stream objects

Example write program that converts 45.6 centigrade temperature into Fahrenheit using the formula F=9/5(C+32) and displays the temperature in Fahrenheit using cout stream object:

c++ Stream objects

Example write a program which converts age in months and days using cout stream object:

c++ Stream objects

Example write a program that displays values of integer, floating point variable and character variable on the screen using cout stream object:

c++ Stream objects


Example write a program that calculates the distance covered by a body in 20.5 seconds. The initial velocity of body is 15.8 meter per second and acceleration is 10.2m/sec2 using cout stream object:

Formula:

S=vit+1/2at2

c++ Stream objects

 

Recommended For You

Fawad

About the Author: Fawad

My name is Shahzada Fawad and I am a Programmer. Currently, I am running my own YouTube channel "Expertstech", and managing this Website. My Hobbies are * Watching Movies * Music * Photography * Travelling * gaming and so on...

Leave a Reply

%d bloggers like this: