Tishk International University
Faculty of Engineering
Computer Engineering Department
Expressions and Interactivity
Mahmood Yashar Hamza
Programming Fundamentals 4
CMPE105
Spring 2025
Previous lesson
✓ Special Characters
✓ Numbering System
✓ Identifiers
✓ Integer Literals
✓ Floating-Point Literals
✓ Boolean Literals
✓ Character Literals
✓ String Literals
✓ Arithmetic operators and expressions 2
Lessons Outline
✓ The cin Object
✓ Mathematical Expressions
✓ Overflow and Underflow
✓ Type Casting
✓ Multiple Assignment
✓ Combined Assignment
✓ Characters and string Objects
✓ Mathematical Library Functions
3
Learning Outcomes
✓ Understand cin operator in C++
✓ Understand the mathematical operators and overflow, underflow
✓ Learn the String objects in C++
✓ Learn different mathematical library functions
4
Textbook Source
✓ The Tony Gaddis, Starting Out with C++: From Control Structures through Objects,
8th Edition
➢ Chapter 3 (from page 83 to 147)
5
The Cin Object
✓ Data is often input from the standard input stream object cin (std::cin) which is
normally the keyboard and the stream extraction operator, >>, in C++.
cin >> ...;
✓ The std::cout and std::cin stream objects facilitate interaction between the user and
the computer. Because this interaction resembles a dialog, it’s often called
conversational computing or interactive computing
6
The Cin Object
✓ When using cin , you must identify variable or a list of variables next to the
extraction (>>) operator.
int a;
cin >> a;
✓ The first statement declares a variable of type int called a, and the second one
waits for an input from cin (the keyboard) in order to store it in this integer
variable.
✓ cin can only process the input from the keyboard once the enter key has been
pressed.
✓ Extraction (getting data) from cin will not be done until the user presses enter
key after the typing completed. 7
The Cin Object
#include <iostream>
using namespace std;
int main()
{
int length, width, area;
cout << "This program calculates the area of a ";
cout << "rectangle.\n";
cout << "What is the length of the rectangle? ";
cin >> length;
cout << "What is the width of the rectangle? ";
cin >> width;
area = length * width;
cout << "The area of the rectangle is " << area << ".\n";
return 0; 8
The Cin Object
✓ The cin object causes a program to wait until data is typed at the keyboard and
the [Enter] key is pressed. No other lines in the program will be executed until
cin gets its input.
✓ cin automatically converts the data read from the keyboard to the data type of the
variable used to store it.
✓ cin is smart enough to know this will have to be converted to an int value before it is
stored in the length variable. 9
Entering Multiple Values
#include <iostream>
using namespace std;
int main()
{
int length, width, area;
cout << "This program calculates the area of a rectangle.\n";
cout << "Enter the length and width of the rectangle ";
cout << "separated by a space.\n";
cin >> length >> width;
area = length * width;
cout << "The area of the rectangle is " << area << ".\n";
return 0;
}
10
Mathematical Expressions
#include <iostream>
using namespace std;
int main()
{
double numerator, denominator;
cout << "This program shows the decimal value of a fraction.\n";
cout << "Enter the numerator: ";
cin >> numerator;
cout << "Enter the denominator: ";
cin >> denominator;
cout << "The decimal value is ";
cout << (numerator / denominator) << endl;
return 0;
}
11
Mathematical Library Functions
✓ C++ does not have an exponent operator. Raising a number to a power
requires the use of a library function. The C++ library isn’t a place where you
check out books, but a collection of specialized functions.
✓ Think of a library function as a “routine” that performs a specific operation.
✓ One of the library functions is called pow, and its purpose is to raise a number to a
power.
✓ The statement area = pow(4.0, 2.0) is equivalent to the following
algebraic statement: area = 42 12
Mathematical Library Functions
#include <iostream>
#include <cmath> // needed for pow function
using namespace std;
int main()
{
const double PI = 3.14159;
double area, radius;
cout << "This program calculates the area of a circle.\n";
cout << "What is the radius of the circle? ";
cin >> radius;
area = PI * pow(radius, 2.0);
cout << "The area is " << area << endl;
return 0;
}
13
Overflow and Underflow
✓ Trouble can arise when a variable is being assigned a value that is too large for its
type.
✓ Here is a statement where a, b, and c are all short integers:
a = b * c;
✓ If b and c are set to values large enough, the multiplication will produce a number too
big to be stored in a. To prepare for this, a should have been defined as an int, or a
long int.
✓ When a variable is assigned a number that is too large for its data type, it overflows.
✓ Likewise, assigning a value that is too small for a variable causes it to underflow.
14
Overflow and Underflow
#include <iostream>
using namespace std;
int main()
{
// testVar is initialized with the maximum value for a short.
short testVar = 32767;
// Display testVar.
cout << testVar << endl;
// Add 1 to testVar to make it overflow.
testVar = testVar + 1;
cout << testVar << endl;
// Subtract 1 from testVar to make it underflow.
testVar = testVar - 1;
cout << testVar << endl;
return 0;
} 15
Type Casting
✓ A type cast expression lets you manually promote or demote a value. The general
format of a type cast expression is
static_cast<DataType>(Value)
(DataType)Value
✓ where Value is a variable or literal value that you wish to convert and DataType is
the data type you wish to convert Value to. Here is an example of code that uses a
type cast expression:
double number = 3.7;
int val;
val = static_cast<int>(number); 16
Type Casting
#include <iostream>
using namespace std;
int main()
{
int books; // Number of books to read
int months; // Number of months spent reading
double perMonth; // Average number of books per month
cout << "How many books do you plan to read? ";
cin >> books;
cout << "How many months will it take you to read them? ";
cin >> months;
perMonth = static_cast<double>(books) / months;
// perMonth = (double) books / months;
cout << "That is " << perMonth << " books per month.\n";
return 0;
}
17
Multiple Assignment & Combined assignment
✓ C++ allows you to assign a value to multiple variables at once. If a program has
several variables, such as a, b, c, and d, and each variable needs to be assigned a
value:
a = b = c = d = 12;
✓ The expression on the right side of the assignment operator gives the value of
number plus 1. The result is then assigned to number, replacing the value that was
previously stored there. Effectively, this statement adds 1 to number
number = number + 1;
18
Formatting Output (setw)
✓ cout offers a way of specifying the minimum number of spaces to use for each
number. A stream manipulator, setw, can be used to establish print fields of a
specified width:
value = 23;
cout << "(" << setw(5) << value << ")";
✓ In the example below, the number 23 will be displayed in a field of 5 spaces. Since
23 only occupies 2 positions on the screen, 3 blank spaces will be printed before it.
To further clarify how this works, look at the following statements:
This will cause the following output:
( 23)
19
Formatting Output (setw)
✓ The way a value is printed is called its formatting.
✓ The cout object has a standard way of formatting variables of each data type.
Sometimes, however, you need more control over the way data is displayed.
720
720.0
720.00000000
7.2e+2
+720.0
20
Formatting Output (setw)
#include <iostream>
using namespace std;
int main()
{
int num1 = 2897, num2 = 5, num3 = 837,
num4 = 34, num5 = 7, num6 = 1623,
num7 = 390, num8 = 3456, num9 = 12;
// Display the first row of numbers
cout << num1 << " " << num2 << " " << num3 << endl;
// Display the second row of numbers
cout << num4 << " " << num5 << " " << num6 << endl;
// Display the third row of numbers
cout << num7 << " " << num8 << " " << num9 << endl;
return 0;
}
21
Formatting Output
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int num1 = 2897, num2 = 5, num3 = 837,
num4 = 34, num5 = 7, num6 = 1623,
num7 = 390, num8 = 3456, num9 = 12;
// Display the first row of numbers
cout << setw(6) << num1 << setw(6)
<< num2 << setw(6) << num3 << endl;
// Display the second row of numbers
cout << setw(6) << num4 << setw(6)
<< num5 << setw(6) << num6 << endl;
// Display the third row of numbers
cout << setw(6) << num7 << setw(6)
<< num8 << setw(6) << num9 << endl;
return 0;
} 22
Formatting Output
#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;
}
23
Formatting Output (setprecision)
✓ Floating-point values may be rounded to a number of significant digits, or
precision, which is the total number of digits that appear before and after the
decimal point.
✓ You can control the number of significant digits with which decimal values
are displayed using set precision manipulator
#include<iostream>
#include <iomanip>
using namespace std;
int main()
{
double quotient, number1 = 132.364, number2 = 26.91;
quotient = number1 / number2;
cout << quotient << endl;
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; 24
}
Formatting Output (setprecision)
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
double pi = 3.14159265359;
cout << "Default precision: " << pi << endl;
cout << "Precision 3: " << setprecision(3) << pi << endl;
cout << "Precision 5: " << setprecision(5) << pi << endl;
return 0;
}
25
Formatting Output (setprecision)
26
Formatting Output (showpoint)
✓ Floating-point numbers are not displayed with trailing zeroes, and floating-point
numbers that do not have a fractional part are not displayed with a decimal point.
For example, look at the following code.
#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;
}
27
Formatting Output (fixed)
✓ The setprecision manipulator can sometimes surprise you in an undesirable way.
When the precision of a number is set to a lower value, numbers tend to be
printed in scientific notation.
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
double x = 145678.99;
cout << setprecision(2);
cout << x << endl;
cout << setprecision(2) << fixed;
cout << x << endl;
return 0;
} 28
Formatting Output (left and right)
✓ By default, the values after setw are right-justified but you can
✓ cause the values to be left-justified by using the left
manipulator, as shown in the following code.
#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;
cout << left << setw(10) << x << endl;
cout << setw(10) << y << endl;
cout << setw(10) << z << endl;
return 0; 29
}
Character and string object
✓ It is possible to use cin with the >> operator to input strings, it can cause
problems that you need to be aware of.
✓ When cin reads input, it passes over and ignores any leading whitespace
characters (spaces, tabs, or line breaks).
✓ Once it comes to the first nonblank character and starts reading, it stops
reading when it gets to the next whitespace character
✓ To work around this problem, you can use a C++ function named getline.
✓ The getline function reads an entire line, including leading and embedded
spaces, and stores it in a string object.
30
Character and string object
#include <iostream>
using namespace std;
int main()
{
string name, city;
cout << "Please enter your name: ";
cin >> name;
cout << "Enter the city you live in: ";
cin >> city;
cout << "Hello, " << name << endl;
cout << "You live in " << city << endl;
return 0;
}
31
Character and string object
#include <iostream>
#include <string>
using namespace std;
int main()
{
string name, city;
cout << "Please enter your name: ";
getline(cin, name);
cout << "Enter the city you live in: ";
getline(cin, city);
cout << "Hello, " << name << endl;
cout << "You live in " << city << endl;
return 0;
}
32
Random Numbers
✓ Random numbers are useful for lots of different programming tasks. The C++
library has a function, rand(), that you can use to generate random numbers.
#include <iostream>
#include <cstdlib> // For rand and srand
#include <ctime> // For the time function
using namespace std;
int main()
{
// Get the system time.
unsigned seed = time(0);
// Seed the random number generator.
srand(seed);
// Display three random numbers.
cout << rand() << endl;
cout << rand() << endl;
cout << rand() << endl;
return 0; 33
}
More Mathematical Library Functions
34
Next Lecture
✓ Relational operators
✓ The if statement
✓ The if/else statement
✓ Nested if statements
✓ The if/else if statement
✓ Flags
✓ Logical operators
✓ The conditional operator
✓ The switch statement
✓ Blocks and variable scope
35
Thanks
36