C++ Stream: Basic Input/output Stream objects in C++ with example codes
Table of Contents
C++ stream Description:
C++ Stream Objects- In this article you will learn how to use cin stream object and cout stream object in C++ programming
Amazon Purchase Links:
*Please Note: These are affiliate links. I may make a commission if you buy the components through these links. I would appreciate your support in this way!
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 the input device into the main memory or from the main memory to the 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 is declared in the header file “iostream.h”. in most of the c++ program, this header file( iostream.h) must be included 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 an input statement to get input from user through keyword during the execution of the 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 part of iostream.h header file.
When an input statement is executed, the computer waits to get input from the keyboard. When a value is typed and the 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 the standard input C++ stream object.
>:
It is known as an extraction operator or get from the operator. It takes input from 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:
- The following statement inputs a value from the keyboard and stores it in variable marks during the execution of the program:
cin>>marks;
- The 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, the user can enter the values by using one of the following ways:
- Type value for the first variable and press the enter key. The computer will wait to get the value for the second variable. Repeat this process until values in all variables have been entered.
OR
- Type the values of all variables separated by spaces and then press the 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:
1 2 3 4 5 6 7 8 9 10 11 |
#include <iostream> using namespace std; main() { char ch; int asc; cout<<"Enter the character: "; cin>>ch; asc=ch; cout<<"ASCII code of "<<ch<<"is: " <<asc; } |
1 2 3 |
Output: Enter the character: B ASCII code of B is 66 |
Write a program that inputs time in seconds and converts it into a standard format (such as hh:mm:ss format) using cin cout stream objects:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
#include <iostream> using namespace std; main() { int seconds, ss,mm,hh; cout<<"Enter time in second: "; cin>>seconds; hh=seconds/3600; seconds=seconds%3600; mm=seconds/60; ss=seconds%60; cout<<"time in hh:mm:ss format is: "; cout<<hh<<":"<<mm<<":"<<ss; } |
1 2 3 |
Output: Enter time in second: 5000 time in hh:mm:ss format is: 1:23:20 |
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:
1 2 3 4 5 6 7 8 9 10 |
#include <iostream> using namespace std; main() { float petrol, distance; cout<<"Enter petorl in liter: "; cin>>petrol; distance=5.3*petrol; cout<<"car will cover distance "<<distance<<" miles"; } |
1 2 3 |
Output: Enter petorl in liter: 45 car will cover distance 238.5 miles |
Example: write a program that inputs the name, age, height and gender of a student using ‘cin C++ stream object’:
Flow chart:
Programming:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
#include <iostream> using namespace std; int main() { char name[16], gender[5]; float height; int age; cout<<"Enter Name of the Student: "; cin>>name; cout<<"Enter Age of the Student: "; cin>>age; cout<<"Enter Height of the Student: "; cin>>height; cout<<"Enter Sex of the Student: "; cin>>gender; cout<<"\n-------- Student Detail -----------"; cout<<"\nName of the student is: "<<name<<endl; cout<<"Age of the student is: "<<age<<endl; cout<<"Height of the student is: "<<height<<endl; cout<<"Sex of the student is: "<<gender<<endl; } |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
#include <iostream> using namespace std; int main() { float km, miles; cout<<"Enter miles: "; cin>>miles; km=1.609 * miles; cout<<miles<<" Miles = "<<km<<" Kilometers"; } |
Example: write a program in C++ to converts pound into kilograms using cin cout stream objects:
1 pound = 0.453592
1 2 3 4 5 6 7 8 9 10 11 |
#include <iostream> using namespace std; int main() { double kg, pounds; cout<<"Enter in pounds: "; cin>>pounds; kg=0.453592 * pounds; cout<<pounds<<" Pounds = "<<kg<<" Kilograms"; } |
Example: write a program in C++ which convert Inches to centimeters using cin cout stream objects:
1 inch = 2.54 centimeters
1 2 3 4 5 6 7 8 9 10 11 |
#include <iostream> using namespace std; int main() { float inches, cm; cout<<"Enter in inches: "; cin>>inches; cm=2.54 * inches; cout<<inches<<" Inches = "<<cm<<" Centimeters"; } |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
#include <iostream> using namespace std; int main() { float r, area,circum; float pi = 3.1417; cout<<"Enter radius of sphere: "; cin>>r; area= 4.0 *pi*r*r; circum=(4.0/3.0)*pi*r*r*r; cout<<"Area of sphere = "<<area<<endl; cout<<"Circumference of sphere = "<<circum; } |
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:
Program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
#include <iostream> using namespace std; int main() { float r, area, circum, pi=3.1417; cout<<"Enter Radius of circle: "; cin>>r; area= pi*r*r; circum= 2*pi*r; cout<<"Area of circle = "<<area<<endl; cout<<"Circumference = "<<circum; } |
Example: write a temperature program in C++ which converts the Fahrenheit to Centigrade using cin cout stream objects:
C=5/9(F-32)
Flowchart:
Program:
1 2 3 4 5 6 7 8 9 10 11 |
#include <iostream> using namespace std; int main() { float f,c; cout<<"Enter Temperature in Fahrenheit: "; cin>>f; c= 5/9.0 * (f-32); cout<<"Temperature in Centigrade: "<<c; } |
Example: write a program that inputs marks obtained by a student in five different subjects and find out aggregate marks (total marks) and percentage using cin cout stream objects:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
#include <iostream> using namespace std; int main() { float math, cpp, os, dbms, eng, total, avg; cout<<"Enter marks of Mathematics: "; cin>>math; cout<<"Enter marks of CPP: "; cin>>cpp; cout<<"Enter marks of OS: "; cin>>os; cout<<"Enter marks of DBMS: "; cin>>dbms; cout<<"Enter marks of English: "; cin>>eng; total= math+os+cpp+dbms+eng; avg= total/5.0; cout<<"\nTotal marks: "<<total<<endl; cout<<"\nAverage marks: "<<avg<<endl; } |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
#include <iostream> #include<math.h> using namespace std; int main() { float a,b,c,s,area; cout<<"Enter first side f triangle: "; cin>>a; cout<<"Enter second side f triangle: "; cin>>b; cout<<"Enter third side f triangle: "; cin>>c; s=(a+b+c)/2.0; area = sqrt(s*(s-a)*(s-b)*(s-c)); cout<<"\nArea of triangle = "<<area; } |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 |
#include <iostream> using namespace std; int main() { float base, per,area; cout<<"Enter value of Base: "; cin>>base; cout<<"Enter value of Per: "; cin>>per; area=(base*per)/2.0; cout<<"Area of triangle = "<<area; } |
Example: write a program in C++ which reverse the order of the long numbers using cin cout stream objects:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#include <iostream> using namespace std; int main() { long n, a, b,c; cout<<"Enter Numbers: "; cin>>n; a=n/1000; n=n%1000; b=n/100; n=n%100; c=n/10; n=n%10; cout<<"Long number in reverse order: "<<n<<c<<b<<a; } |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
#include <iostream> using namespace std; int main() { float elect, gas; cout<<"Enter the current price of electricity: "; cin>>elect; cout<<"Enter the current price of gas: "; cin>>gas; cout<<"New electricity rate = "<<elect+elect*20/100; cout<<"\nNew gas rate = "<<gas+gas*20/100; } |
write a program which calculates the employee dearness allowance, medical allowance, house rent allowance and calculates 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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
#include <iostream> using namespace std; int main() { double basic, da, ma, hr, gross; cout<<"Enter basic salary: "; cin>>basic; da=0.25*basic; ma = 0.15*basic; hr = 0.45*basic; gross= basic+da+ma+hr; cout<<"Gross salary = "<<gross; } |
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 the display screen. It is a part of iostream.h header file.
The general syntax of cout is as follows:
cout<<const1/var1/exp1[<<consta2/var2/exp2/….];
where
cout:
it is the name of the output C++ stream object, which is used to display the standard output.
<<:
It is known as an insertion operator or put to the 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 value of multiple variables or constants.
Const1/var1/exp1, Const2/var2/exp2:
It indicates the list of constants, variables or arithmetic. Expression. for each constant, or variable, or expression. separated insertion operator (<<) is used. The string constant is given in double quotation marks. A character constant is given in single quotation marks. However, variables, numeric constants, and expression are given without quotation marks.
Examples:
- The following statement will display a message “Electronic Clinic”:
Cout<<”Electronic Clinic”;
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.
- The following statement will display the value of the variable ‘sum’ on the screen.
Cout<<sum;
Please note that in this output statement the variable ‘sum’ is not enclosed with any quotation marks.
- The 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
Example: write a program that computes and displays the area of a square. The formula to compute the area of the square is using cout stream object:
Area = height * width;
Flowchart:
Program:
1 2 3 4 5 6 7 8 9 10 11 12 |
#include <iostream> using namespace std; int main() { int height, width, area; height = 15; width= 3; area = height * width; cout<<"Area of square ="<<area; } |
It the above program “Area of square =” is a text message. The text message will be displayed first and the value of the 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:
1 2 3 4 5 6 7 8 9 10 11 |
#include <iostream> using namespace std; int main() { float a, b,s; a=20.3; b=50.2; s=a+b; cout<<"Sum of "<<a<<" and "<<b<<" is ="<<s; } |
Example: write a 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:
1 2 3 4 5 6 7 8 9 10 |
#include <iostream> using namespace std; int main() { float f,c; c=45.6; f=9/5.0*c+32; cout<<c<<" Centigrade = "<<f<<" Fahrenheit"; } |
Example: write a program which converts age into moths and days using cout stream object:
1 2 3 4 5 6 7 8 9 10 11 12 |
#include <iostream> using namespace std; int main() { float age, days,months; age=20; months=age*12; days= age*365; cout<<"Age in months = "<<months<<endl; cout<<"Age in days = "<<days<<endl; } |
Example: write a program that displays values of integer, floating-point variable, and character variable on the screen using cout stream object:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
#include <iostream> using namespace std; int main() { int a= 88; float b = 44.3; char ch= 'P'; cout<<"Value of Integer variable is: "<<a<<endl; cout<<"Value of Floating point variable is: "<<b<<endl; cout<<"Value of Character variable is: "<<ch<<endl; } |
Example: write a program that calculates the distance covered by a body in 20.5 seconds. The initial velocity of the body is 15.8 meter per second and acceleration is 10.2m/sec2 using cout stream object:
Formula:
S=vit+1/2at2
Program:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
#include <iostream> using namespace std; int main() { float vi, a, t, s; t=20.5; vi=15.8; a=10.2; s=vi*t+1/2.0*a*t*t; cout<<"Distance covered = "<<s<<" meters"; } |