You are on page 1of 31

C++ Programming

CHAPTER 3
ASSIGNMENT, FORMATTING, & INTERACTIVE
INPUT

USCS20/Aizat
Objectives
2

Assignment Operations
Formatting Numbers for Program Output
Using Mathematical Library Functions
Program Input Using the cin Object
Symbolic Constants

USCS20/Aizat
Assignment Operations
3

Assignment statement  =
Shortcuts  +=, -=, *=, /=, %=
Coercion
Accumulative statement
Counting statement
 Increment (++)  prefix, postfix
 Decrement (--) prefix, postfix

USCS20/Aizat
Assignment Operations (continued)
4

Right side of an assignment statement may contain


any expression that can be evaluated to a value
Examples:
newtotal = 18.3 + total;

Assignment operator expression

Only one variable can be on the left side of an


assignment statement

USCS20/Aizat
Assignment Operations (continued)
5

Write a program to calculate area of a triangle?


start
#include <iostream.h>

Input int main()


length & {
height
double length, height, area;
length = 2.5;
Calculate height = 16.0;
area area = ½*(length * height);

cout<<“Area of triangle is: “<<area;


Display
area return 0;
}
end
USCS20/Aizat
Assignment Operations (continued)
6

 C++ statement: any expression terminated by a


semicolon
 Multiple assignments in the same expression are possible
Example:
a = b = c = 25;

25 25 25
a b c

USCS20/Aizat
Assignment Operations:
Short cuts
7

Additional assignment operators provide short cuts:


+=, -=, *=, /=, %=
Example:

total = total +5 ; total += 5;


total = total - 5 ; total -= 5;
total = total *5 ; total *= 5;
total = total /5 ; total /= 5;
total = total %5 ; total %= 5;

USCS20/Aizat
Assignment Operations:
Coercion
8

Coercion: forcing a data value to another data type


Value of the expression on the right side of an
assignment statement will be coerced (converted) to
the data type of the variable on the left side during
evaluation
Example:
 int temp; Right
side 27
 temp = 27.5;
temp

USCS20/Aizat
Assignment Operations:
Accumulative Statement
9

 Accumulation statement: has the effect of


accumulating, or totaling
 Syntax:
 variable = variable + newValue;
Refer to program 3.3

USCS20/Aizat
Output?
10

USCS20/Aizat
Assignment Operations:
Counting Statement
11

Counting statement: adds a fixed value to the


variable’s current value
Syntax:
variable = variable + fixedNumber;
Example:
i = i + 1;
count = count + 1;

USCS20/Aizat
Assignment Operations:
Counting Statement (cont.)
12

Increment operator ++: unary operator for the


special case when a variable is increased by 1
Decrement operator --: unary operator for the
special case when a variable is decreased by 1
Prefix increment operator appears before the
variable
Example: ++i; --i;
Postfix increment operator appears after the
variable
Example: i++, i--;

USCS20/Aizat
Assignment Operations:
Counting Statement (cont.)
13

Prefix Equivalent to output


k = ++n; n = n+1; If n =1;
k = n; k starts from 2, 3 ,4,…
k = --n; n = n-1; If n =5;
k = n; k starts from 4, 3, 2,…

Postfix Equivalent to output


k = n++; k = n; If n =1;
n = n+1; k starts from 1, 2 ,3,…
k = n--; k = n; If n =5;
n = n-1; k starts from 5, 4, 3,…

USCS20/Aizat
Objectives
14

Assignment Operations
Formatting Numbers for Program Output
Using Mathematical Library Functions
Program Input Using the cin Object
Symbolic Constants

USCS20/Aizat
Formatting Numbers for
Program Output
15

Proper output formatting contributes to ease of use


and user satisfaction
cout with stream manipulators can control output
formatting
#include <iomanip.h> as library / header file must
be included to use manipulators requiring
arguments.
Refer to Table 3.1 Commonly Used Stream
Manipulators

USCS20/Aizat
16

USCS20/Aizat
Formatting Numbers for
Program Output (cont.)
17

USCS20/Aizat
Formatting Numbers for
Program Output (cont.)
18

Formatting floating-point numbers requires three field-


width manipulators to
 Set the total width of the display
 Force a decimal place
 Set the number of significant digits after the decimal point
Example:
cout << “|”<< setw(10) <<fixed<< setprecision(3) << 85.97 << “|”;
| 85.970|
produces this output:

Table 3.2 will shows Effect of Format Manipulators


Table 3.3 shows Format Flags for Use with setiosflags()

USCS20/Aizat
Objectives
19

Assignment Operations
Formatting Numbers for Program Output
Using Mathematical Library Functions
Program Input Using the cin Object
Symbolic Constants

USCS20/Aizat
Using Mathematical Library Functions
20

C++ has preprogrammed mathematical functions


that can be included in a program
You must include the cmath header file:
#include <cmath>
Math functions require one or more arguments as
input, but will return only one value
All functions are overloaded, and can be used with
integer and real arguments

USCS20/Aizat
Using Mathematical Library Functions (cont.)
21

Table 3.5 Common C++ Functions

USCS20/Aizat
Using Mathematical Library Functions (cont.)
22

To use a math function, give its name and pass the
input arguments within parentheses
Expressions that can be evaluated to a value can be
passed as arguments
pow ( num1, num2);

Function name Data passed to the function

pow(5,2);  25

USCS20/Aizat
Objectives
23

Assignment Operations
Formatting Numbers for Program Output
Using Mathematical Library Functions
Program Input Using the cin Object
Symbolic Constants

USCS20/Aizat
Program Input Using the cin Object
24

cin Object: allows data entry to a running program


Use of the cin object causes the program to wait for
input from the keyboard
When keyboard entry is complete, the program
resumes execution, using the entered data
An output statement preceding the cin object
statement provides a prompt to the user

USCS20/Aizat
Program Input Using the cin Object (cont.)
25

#include <iostream.h>

int main() Output:


{
double length, height, area; Please enter length: 5
cout<<“Please enter length: “; Please enter height: 10
cin>> length; Area of triangle is: 25
cout<<“Please enter height: “;
cin>> height;

area = 0.5*(length * height);


cout<<“Area of triangle is: “<<area<<endl;

return 0;
}

USCS20/Aizat
Program Input Using the cin Object (cont.)
26

 cin can accept multiple input values to be stored in


different variables
 Multiple numeric input values must be separated by spaces
 Example:
 cin >> num1 >> num2
 with keyboard entry: 0.052 245.79

USCS20/Aizat
27

#include <iostream.h>

int main()
{
double length, height, area;
cout<<“Please enter length and height: “;
cin>> length>> height;

area = ½*(length * height);


cout<<“Area of triangle is: “<<area<<endl;
Output:
return 0;
} Please enter length and height: 5 10
Area of triangle is: 25

USCS20/Aizat
Objectives
28

Assignment Operations
Formatting Numbers for Program Output
Using Mathematical Library Functions
Program Input Using the cin Object
Symbolic Constants

USCS20/Aizat
Symbolic Constant
29

 Symbolic constant: a constant value that is declared


with an identifier using the const keyword
 A constant’s value may not be changed
 Example:
 const int MAXNUM = 100;
 Good programming places statements in appropriate order

USCS20/Aizat
Symbolic Constant (cont.)
30

Proper placement of statements:


preprocessor directives
int main()
{
symbolic constants
main function declarations
other executable statements
return value
}
USCS20/Aizat
Common Programming Errors
31

 Failure to declare or initialize variables before use


 Failure to include the preprocessor statement when using a
C++ preprogrammed library
 Passing the incorrect number or type of arguments to a
function
 Applying increment or decrement operator to an
expression instead of an individual variable
 Failure to separate all variables passed to cin with the
extraction symbol >>
 Failure to test thoroughly

USCS20/Aizat

You might also like