You are on page 1of 12

eÁw

CHAPTER 9
Input and Output operators

OBJECTIVES
To Know the operators used for performing basic input operations
Output operations

187
188
Input and Output operators

9.1 Introduction
After
eÁwunderstanding about the structure of C++ programming and about its data types, it becomes
necessary to understand more about formatting the input and outputs of a program. The input output
operations are done using library functions cin and cout objects of the class iostream. Using the standard
input and output library, we will be able to interact with the user by printing messages on the screen and
getting the user’s input from the keyboard.
C++ makes use of convenient abstraction called streams to perform input and output operations in
sequential media such as the monitor or the keyboard. A stream is an object where a program can either
insert/extract characters to/from it. We do not really need to care about many specifications about the
physical media associated with the stream - we only need to know that it will accept or provide characters
sequentially. The standard C++ library includes the header file iostream, where the standard input and
output stream objects are declared.

9.2 Input Operator “>>”

cin >> Variable


KEYBOARD

Figure 9.1 Input Using cin

The standard input device is usually the keyboard as shown in figure 9.1. Input in C++ is done by
using stream extraction (>>) on the cin stream. The operator must be followed by the variable that will
store the data that is going to be extracted from the stream.

Example 9.1:
int age;
cin>>age;
The first statement in example 9.1 declares a variable of type int called age, and the second one waits
for an input from cin (the keyboard) in order to store it in this integer variable. cin stands for “console
input”. It can only process the input from the keyboard once the RETURN key has been pressed.
Therefore, even if we request a single character, the extraction from cin will not process the input until the
user presses RETURN after the character has been introduced.
We must always consider the type of the variable that we are using as a container with cin extractions.
If we request an integer we will get an integer, if we request a character we will get a character and if we
request a string of characters we will get a string of characters.

189
Input and Output operators

Program 9.1 To find the sum of any two integers


#include <iostream.h>
#include <iomanip.h>
void main()
{
int a, b, sum;
clrscr();
cout<<“Enter two numbers: “;
cin>>a>>b;
sum = a+b;
cout<<“The sum is “<<sum<<endl;
getch();
}
Sample Run: Enter two numbers: 5 -10
The sum is -5

9.3 Output Operator “<<“

MONITOR cout << Variable

Figure 9.2 Output using cout

The standard output device is the screen (monitor) as shown in figure9.2, and outputting in
C++ is done by using the object followed by the “stream insertion” which is written as <<
(two “less than” signs). cout stands for console output.

Example 9.2:

cout<< “ Let us learn C++”; // prints Let us learn C++ on the screen.

The << operator inserts the data that follows it into the stream preceding it. In the examples
above it inserts the constant string “Let us learn C++”. Notice that the sentence in the instruction is
enclosed between double quotes (“), because it is a constant string of characters. Whenever we
want to use constant strings of characters we must enclose them between double quotes (“) so that
they can be clearly distinguished from variable names.

190
Input and Output operators

Example 9.3:
eÁw cout<< “ Compilation”; // prints Compilation
cout<< Compilation; // prints the content of the variable Compilation

In order to perform a line break on the output we must explicitly insert a new-line character into
cout. In C++ a new-line character can be specified as’ \n’ (backslash, n): the newline character is an
escape sequence character and helps in formatting the output statement. We have already discussed
about escape sequences in the previous chapter.

Program 9.2 To demonstrate output


#include <iostream.h>
void main ()
{
<<“C++ makes use of OBJECT ORIENTED PROGRAMMING “<<“\n”;
cout<<“C++ is a superset of C “<<“\n”;
cout<<“Writing Algorithms is great fun “ ;
}
Sample Run: C++ makes use of OBJECT ORIENTED PROGRAMMING
C++ is a superset of C
Writing Algorithms is great fun

Cascading of I/O operators:


C++ supports the use of stream extraction (>>) and stream insertion (<<) operators many times in a
single input (cin) and output (cout) statements. If a program requires more than one input variable then it
is possible to input these variables in a single cin statement using multiple stream extraction operators.
Similarly, when we want to output more than one result then this can be done using a single cout statement
with multiple stream insertion operators. This is called as cascading of input output operators.

Example 9.4:
cout<<“ enter the value for x”;
cin>> x;
cout<<“ enter the value for y”;
cin>>y;
Instead of using cin statement twice, we can use a single cin statement and input the values for the
two variables x and y using multiple stream extraction operator as shown below.
cout<<“ enter the value for x and y”;
cin>>x>>y;
Similarly, we can even output multiple results in a single cout statement using cascading of stream
insertion operator as shown below.
cout<<“ the sum of” <<x<<“ and “<<y<<“=”<<x+y;
191
Input and Output operators

• The multiple uses of input/output operators (<< and >>) in a single


statement is called cascading of input/ output operators.

• The input operator is also known as "stream extraction” operator >>" means
to "get from" is used to read value from the standard input device.

• The output operator is also known as "stream insertion operator << " means
to "put to" is used to send the value to the standard output device.

• cin/cout are keywords which are predefined objects in C++ that corresponds
to standard input output stream.

• Manipulators are operators used along with the insertion operator to


manipulate or modify the output. Some of them are endl and setw.

• endl is same as the escape sequence \n. It causes a line feed to be inserted
into the stream.

• setwcauses the number that follows in the stream to be printed within


ancharacter wide field and the value is right justified within the field.

• iomanip.his aheader file that holds the objects endl and setw.

Program 9.3 Convert the temperature in Fahrenheit into Celsius


#include <iostream.h>
#include<iomanip.h>
void main ()
{
float Fahrenheit, Celsius;
cout<< “Enter the value of Fahrenheit: “;
cin>> Fahrenheit;
Celsius = ((5.0 / 9.0) * Fahrenheit- 32.0);
cout<<Fahrenheit<<“F = “<<Celsius<<“C”<<endl;
}
Sample Run: Enter the value of Fahrenheit: 105
105F = 26.3333C

192
Input and Output operators

Program
Program 9.4 9.5 To interchange
To interchange the values
the values of two variables
of two variables without
using third using third variable
variable
#include<iostream.h>
eÁw #include<iostream.h>
#include<conio.h>
#include<conio.h>
#include<iomanip.h> Practical
Practical
void main()
#include<iomanip.h> program
{ program
intvoida,main()
b, temp;
clrscr();
{
cout<<“ Enter two numbers: “;
cin>>a>>b; int a, b, temp;
cout<<“Before interchanging: a = “<<a<<“ and b= “<<b<<endl;
temp = a;clrscr();
a = b;
cout<<” Enter two numbers: “;
b = temp;
cout<<“After interchanging: a = “<<a<<“ and b= “<<b<<endl;
cin>>a>>b;
getch();
Sample
} Run: cout<<”Before interchanging:
Enter two numbers: 60 100 a = “<<a<<” and b= “<<b<<endl;
Sample Run: Before
aEnter
= a +twob; numbers: 50 100
interchanging: a = 60 and b= 100
Before interchanging: a = 50 and b= 100
bAfter
= a -interchanging:
After b;
interchanging: a =a100 andand
= 100 b= b=
60 50

Program 9.5 To interchange the values of two variables without using third variable
Program 9.5 To interchange the values of two variables without using third variable
#include<iostream.h>
#include<iostream.h>
#include<conio.h>
#include<conio.h>
#include<iomanip.h> Practical
Practical
void main()
#include<iomanip.h> program
{ program
void main()
int a, b;
clrscr();
{
cout<<“ Enter two numbers: “;
cin>>a>>b;
int a, b, temp;
cout<<“Before interchanging: a = “<<a<<“ and b= “<<b<<endl;
a = a + clrscr();
b;
b = a - b;
cout<<” Enter two numbers: “;
a = a – b;
cout<<“After interchanging: a = “<<a<<“ and b= “<<b<<endl;
cin>>a>>b;
getch();
Sample
} Run: cout<<”Before interchanging:
Enter two numbers: 60 100 a = “<<a<<” and b= “<<b<<endl;

Sample Run:Before
a =Enter two numbers: 60 a100
a + interchanging:
b; = 60 and b= 100
Before interchanging: a = 60 and b= 100
bAfter
=After
a -interchanging:
b;interchanging: aa==100
100and
and b=
b= 60
60
193
Input and Output operators

Program
Program 9.6 9.5 Tothe
To find interchange the values ofoftwo
area and circumference variables without using third variable
a circle
#include<iostream.h>
#include<iostream.h>
#include<conio.h>
#include<conio.h>
#include<iomanip.h> Practical
Practical
void main()
#include<iomanip.h> program
{ program
float rad,main()
void area, circum;
clrscr();
{
cout<<“ Enter radius: “;
cin>>rad; int a, b, temp;
area = 3.142 * rad * rad;
circum = 2clrscr();
* 3.142 * rad;
cout<<“Area = “<<area<<endl;
cout<<” Enter two numbers: “;
cout<<“Circumference = “<<circum<<endl;
getch(); cin>>a>>b;
}
Sample Run: Enter cout<<”Before interchanging:
two numbers: 60 100 a = “<<a<<” and b= “<<b<<endl;
Sample Run: Enter radius: 5.5
Area
a = a=+95.0455
Before b;
interchanging: a = 60 and b= 100
Circumference = 34.562
b = a -interchanging:
After b; a = 100 and b= 60

Program
Program 9.7 9.5 To the
To find interchange
area of thethe values
triangle of two
given threevariables
sides without using third variable
#include<iostream.h>
#include<iostream.h>
#include<conio.h>
#include<conio.h>
#include<iomanip.h>
Practical
Practical
#include<math.h>
#include<iomanip.h> program
void main() program
{ void main()
float s1, s2, s3, s, area;
{
clrscr();
cout<<“ Enter the length of three sides: “;
int a, b, temp;
cin>>s1>>s2>>s3;
s = (s1+s2+s3)/2;
clrscr();
area = sqrt(s*(s-s1)*(s-s2)*(s-s3));
cout<<”
cout<<“Area Enter two numbers: “;
= “<<area<<endl;
getch(); cin>>a>>b;
}
Sample Run: cout<<”Before
Sample Run: Enter twolength
Enter the interchanging:
numbers: sides: 5 3 a6 = “<<a<<” and b= “<<b<<endl;
60 100
of three
aArea
=a+
Before
= b;
7.48331
interchanging: a = 60 and b= 100

b = a -interchanging:
After b; a = 100 and b= 60

194
Input and Output operators

Program 9.5 To interchange the values of two variables without using third variable
Program 9.8 To convert days into years, months and days
eÁw #include<iostream.h>
#include<iostream.h>
#include<conio.h>
#include<conio.h>
#include<iomanip.h> Practical
Practical
void main()
#include<iomanip.h> program
{ program
voidintmain()nodays, days, years, months;
clrscr();
{
cout<<“ Enter the total days: “;
cin>>days;
int a, b, temp;
nodays = days;
yearsclrscr();
= days/365;
days = days%365;
cout<<” Enter two numbers: “;
months = days/30;
dayscin>>a>>b;
= days%30;
cout<<nodays<<“days = “<<days<<“days- “<<months<<“months-”
Sample Run: cout<<”Before
Enter two numbers: interchanging:
60 100
<<years<<“years”<<endl;a = “<<a<<” and b= “<<b<<endl;
getch();
a = a + interchanging:
Before b; a = 60 and b= 100
}
Sample Run: bAfter= a -interchanging:
Enter b; total days: 1005
the a = 100 and b= 60
1005days = 5days- 9months-2years

195
Input and Output operators

Program
Program 9.9 9.5 To interchange
To convert thehours,
seconds into values of twoand
minutes variables
secondswithout using third variable
include<iostream.h>
#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
#include<conio.h>
Practical
Practical
void main()
{ #include<iomanip.h> program
program
int main()
void totalsecs, minutes, hours, seconds;
clrscr();
{ cout<<“ Enter the total seconds: “;
cin>>seconds;
int a, b, temp;
totalsecs = seconds;
hoursclrscr();
= seconds/3600;
seconds = seconds%3600;
cout<<” Enter two numbers: “;
minutes = seconds/60;
cin>>a>>b;
seconds = seconds%60;
Sample Run: cout<<”Before
Enter interchanging:
two numbers: 60=100 a = “<<a<<” and b= “<<b<<endl;
cout<<totalsecs<<“seconds “<<seconds<<“seconds-
“<<minutes<<“minutes-”<<hours<<“hours”<<endl;
a = a + interchanging:
Before b; a = 60 and b= 100
getch();
} b = a -interchanging:
After b; a = 100 and b= 60
Sample Run: Enter the total seconds: 10000
10000seconds = 40seconds- 46minutes-2hours

196
Input and Output operators

Review questions:
eÁw
One mark questions:
1. Give the other name for cin( ).
2. Give the other name for cout( ).
3. What is cascading?
4. What are manipulators?
5. Give the header file that holds setw( ) and endl.
6. What is the purpose of setw( ).

Two marks questions:


1. Explain the input operator in c++.
2. Explain the output operator in C++.
3. Explain the cascading of input output operators.

Long answer Questions:


1. Explain the input output operators in C++.
2. Write a C++ program to find the sum of two numbers.
3. Write a C++ program to convert Fahrenheit to Celsius.
4. Write a C++ program to demonstrate the cout .
5. Explain the cascading of input output operators with suitable example.

197
Control Statements

198

You might also like