Sec 03
Sec 03
15
3
324
Equality and Relational operators
Exercises
What is wrong with the following program?
How would you correct it?
•
#include <iostream>
using namespace std;
int main()
{
number = 62.7;
double number;
cout << number << endl;
return 0;
}
Write assignment statements that perform the
following operations with the variables a, b, and c.
#include <iostream>
using namespace std; 0
int main() 100
{
int freeze = 32, boil = 212;
freeze = 0;
boil = 100;
cout << freeze << endl << boil << endl;
return 0;
}
Exercise
#include <iostream>
using namespace std; 8
int main ( ) 2
{
int x = 0, y = 2;
x = y * 4;
cout « x « endl « y « endl ;
return 0;
}
Exercise
Find the Errors
Find the Errors
#include iostream
using namespace std;
int main();
}
int a, b, c \\ Three integers
a=3
b=4
c=a+b
cout < "The value of c is %d" < c;
return 0;
{
Predict the Output
6 3 12
Programs Exercises
prints the circle’s diameter, and area. Use the constant value 3.14159
• Area = π*(radius) ^2
Programs Exercises
middle, and last. Store your initials in these variables and then display
• Write a program that stores your name, address, and phone number in
three separate string objects. Display the contents of the string objects
on the screen.
Programs Exercises
• Write a program that defines an integer variable named age and a float variable
named weight. Store your age and weight, in the variables. The program should
• Program Output
1 - 21
Type Casting
#include <iostream>
using namespace std;
int main()
{
int integer1, integer2;
double result;
integer1 = 19;
integer2 = 2;
result = integer1 / integer2;
cout << result << endl;
result = static_cast<double>(integer1) / integer2;
cout << result << endl;
result = static_cast<double>(integer1 / integer2);
cout << result << endl;
return 0;
}
Find the Errors
Example
26
Combined Assignment operators
Predict the Output Multiple Assignment
6 3 12
Formatting
Formatting Output
The way a value is printed is called its formatting.
C++ has special instructions that allow the user to control
output attributes such as spacing, decimal point precision,
data formatting and other features.
Example:
price =9.5 , rate=8.76
cout << setw(10) << price << setw(7) << rate<<endl;
output:
31
Example
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
int main()
{
int intValue = 3928;
double doubleValue = 91.5;
string stringValue = "John J. Smith";
cout << "(" << setw(5) << intValue << ")" << endl;
cout << "(" << setw(8) << doubleValue << ")" << endl;
cout << "(" << setw(16) << stringValue << ")" <<endl;
return 0;
}
The setprecision Manipulator
The precision is the total number of digits that appear before and after
the decimal point.
The number of significant digits with which floating-point values are
displayed by using the setprecision manipulator.
Syntax of setprecision : setprecision(significant digits)
Example:
33
Example
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
double quotient, number1 = 132.364, number2 = 26.91;
quotient = number1 / number2;
cout << quotient << endl; // quotient= 4.91877
cout << setprecision(5) << quotient << endl;
cout << setprecision(4) << quotient << endl;
cout << setprecision(3) << quotient << endl;
cout << setprecision(2) << quotient << endl;
cout << setprecision(1) << quotient << endl;
return 0;
}
The fixed Manipulator
The fixed Manipulator forces cout to print the digits in fixed-point
notation, or decimal.
float values are written using fixed-point notation, which means the
value is represented with exactly as many digits in the fraction part
as specified by the precision field and with no exponent part.
When the fixed and setprecision manipulators are used
together, the value specified by the setprecision manipulator will
be the number of digits to appear after the decimal point, not
the number of signficant digits.
35
Example
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
double quotient, number1 = 132.364, number2 = 26.91;
quotient = number1 / number2;
cout << quotient << endl; // quotient= 4.91877
cout << fixed;
cout << setprecision(5) << quotient << endl;
cout << setprecision(4) << quotient << endl;
cout << setprecision(3) << quotient << endl;
cout << setprecision(2) << quotient << endl;
cout << setprecision(1) << quotient << endl;
return 0;
}
The show point Manipulator
37
Example 1
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
double x = 123.4, y = 456.0;
cout << setprecision(6) << x << endl;
cout << y << endl;
return 0;
}
Example 2
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
double x = 123.4, y = 456.0;
cout << setprecision(6) <<showpoint << x << endl;
cout << y << endl;
return 0;
}
The left and right Manipulators
39
Example 1
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
double x = 146.789, y = 24.2, z = 1.783;
cout << setw(10) << x << endl;
cout << setw(10) << y << endl;
cout << setw(10) << z << endl;
return 0;
}
Example 2
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
double x = 146.789, y = 24.2, z = 1.783;
cout << left << setw(10) << x << endl;
cout << setw(10) << y << endl;
cout << setw(10) << z << endl;
return 0;
}
Manipulators are operators used in C++ for formatting output.
Stream Manipulator Description
setw(n) Establishes a print field of n spaces.
Displays floating-point numbers in fixed
Fixed point notation.
Causes a decimal point and trailing zeroes to
showpoint be displayed, even if there is no fractional
part.
setprecision (n) Sets the precision of floating-point numbers.
left Causes subsequent output to be left justified.
right Causes subsequent output to be right justified.
41