Professional Documents
Culture Documents
Lab 03
Lab 03
setw manipulator, Input using cin, Arithmetic Operators and Expressions, Type conversion, Assignment
statement, Increment & decrement operators
Output Manipulators
Output manipulators are operators used with the insertion operators (<<) to control or
manipulate how data is displayed. Manipulators are the most common way to control
output formatting. They are used to make the output data look appealing and make
programs user friendly. All the output manipulators in C++ are defined under the
‘iomanip.h’ header file.
The ‘setw ‘manipulator is used to set the field width for the next insertion operator. It
applies only to the next element inserted in the output. We can use either the left or the
right direction to justify the data appropriately in the field. Output is right justified by
default. The default field width is just wide enough to hold the expression. The ‘setw’
operator overcomes this problem by allowing a fixed field width. Let us look at the following program
to understand how ‘setw’ overcomes the problems posed by default field widths.
# include<iostream.h>
# include<iomanip.h>
int main(void)
{
int Durban=4000, Moscow=5000, Sydney=10, Paris=1;
cout<<" City "<<"\t No. of Schools";
cout<<"\n Durban \t"<<Durban;
cout<<"\n Moscow \t"<<Moscow;
cout<<"\n Sydney \t"<<Sydney;
cout<<"\n Paris \t"<<Paris;
return 0;
}
You will observe, it is hard to read and compare these numbers. It would be better if they are right justified. Let’s use
the ‘setw’ manipulator to eliminate the problem by specifying field widths to the respective columns.
# include<iostream.h>
# include<iomanip.h>
void main()
{
int Durban=4000, Moscow=5000, Sydney=10, Paris=1;
cout<<setw(10)<<" City "<<setw(30)<<" No. of Schoolss \n";
cout<<setw(10)<<" Durban"<<setw(20)<<Durban<<"\n";
cout<<setw(10)<<" Moscow"<<setw(20)<<Moscow<<"\n";
cout<<setw(10)<<" Sydney"<<setw(20)<<Sydney<<"\n";
cout<<setw(10)<<" Paris"<<setw(20)<<Paris<<"\n";
return 0;
}
The cin Statement
Just as we have the ‘cout’ statement to display data on the screen, the ‘cin’ statement is
used to extract data from the user or insert data into a variable. The statement is used with
the extraction operators (>>). It is also defined under the <iostream.h> header file and has
the following syntax:
Syntax: cin>>variable1>>variable2…………….;
You may observe that we have used the extraction operator repeatedly ‘>>’ in the syntax.
This is perfectly valid and is known as cascading of operators. For example,
cin>>variable1>>variable2;
cin>>variable1;
cin>>variable2;
To understand how the ‘cin’ statement works let us create a program which enters two
integers from the user and displays their sum on the screen.
# include <iostream.h>
void main()
{
int a,b;
cout<<" Enter the two integers";
cin>>a>>b;
cout<<" The sum of the two integers "<<a<<" and "<<b<<" is "<<a+b;
return 0;
}
Note: Both ‘cin’ and ‘cout’ refer to the basic input/output devices usually keyboards and
monitors.
Arithmetic operators in C++
#include <iostream.h>
int main(void)
{
int a = 10, b=3;
cout << ” Sum = ” << a + b << endl;
cout << ” Difference = “<< a – b << endl;
cout << ” Product = “<<a * b <<endl;
cout << ”Quotient = “<<a /b << endl;
cout << ” Remainder = “<< a %b << endl;
return 0;
}
#include <iostream.h>
int main(void)
{
float salary;
cout<<"\aEnter your desired monthly salary:";
cout<<" Rs._______\b\b\b\b\b\b\b";
cin>>salary;
cout<<"\n\t"<<"Rs."<<salary<<" a month is Rs."<< salary * 12.0<<" a year.";
cout<<"\rGee!\n";
return 0;
}
Type Conversion
# include <iostream.h>
# include <conio.h>
int main(void)
{
clrscr();
int a=10;
float b=4.23;
double c;
c=a*b;
cout<<" c: "<<c;
}
C++ is very flexible when treating mixed datatypes. In C++, it is perfectly normal to perform any arithmetic operations
on variables of different data types. This is achieved by a process called ‘Automatic Type Conversion’. Such a
conversion takes place when two operands of different data types are encountered in the same expression. In such
a case the variable of lower data type is automatically converted to the variable with the higher data type. The following
Table gives the order of data types in C++ according to which type conversions take place.
Look at the following program to understand how type casting really works.
# include <iostream.h>
# include <conio.h>
int main(void)
{
clrscr();
int A=10;
float B=4.2;
float C=A/B;
cout<<"C:"<<C;
return 0;
}
So what happens when this program is executed. As we have already learnt that when the compiler confronts with
such mixed expressions in same statement it converts the variable belonging to the lower data type to the variable
belonging to the higher data type. That is in our program the variable ‘A’ is converted to a float variable and then
divided by the float variable ‘B’ so that the it can be assigned to the float variable ‘C.’ If suppose variable ‘C’ was of
type ‘double’ then both these variables ‘A’ and ‘B’ would get converted to type ‘double’.
The entire process is also explained in the following figure;
Assignment Statement/Operator:
The following table lists the assignment operators supported by the C language
= Simple assignment operator. Assigns values from right side C = A + B will assign the
operands to left side operand value of A + B to C
+= Add AND assignment operator. It adds the right operand to the left C += A is equivalent to C =
operand and assign the result to the left operand. C+A
/= Divide AND assignment operator. It divides the left operand with the C /= A is equivalent to C = C
right operand and assigns the result to the left operand. /A
#include <iostream.h>
#include<conio.h>
int main(void)
{
int a = 21;
int c ;
clrscr();
c = 200;
cout<<"Value of a ="<<a<<" and value of c="<<c<<" before operation"<<endl;
c %= a;
cout<<"(c %= a) Value of c = "<<c<<"\n";
return 0;
}
INCREMENT AND DECREMENT OPERATORS
Increment operators are used to increase the value of the variable by one and decrement operators are used to
decrease the value of the variable by one in C programs.
Syntax: Increment operator: ++var_name; (or) var_name++;
Decrement operator: – -var_name; (or) var_name – -;
Example:
Increment operator : ++ i ; i ++ ;
Decrement operator: – – i ; i – – ;
Below table will explain the difference between pre/post increment and decrement operators in C programming
language.
Operator Operator/Description
EXAMPLE
#include <iostream.h>
#include<conio.h>
int main(void)
{
int a = 20;
int b=0;
clrscr();
b=a++;
cout<<"After b=a++ Value of b ="<<b<<" and value of a="<<a<<endl;
b=++a;
cout<<"After b=++a Value of b ="<<b<<" and value of a="<<a<<endl;
b=a--;
cout<<"After b=a-- Value of b ="<<b<<" and value of a="<<a<<endl;
b=--a;
cout<<"After b=--a Value of b ="<<b<<" and value of a="<<a<<endl;
return 0;
}