Chapter 11
Chapter 11
int height;
cout << "How tall is the room? ";
cin >> height;
The cin Object in Program 3-1
The cin Object
• Can be used to input more than one value:
cin >> height >> width;
Mathematical Expressions
Mathematical Expressions
• Can create complex expressions using multiple
mathematical operators
• An expression can be a literal, a variable, or a
mathematical combination of constants and variables
• Can be used in assignment, cout, other statements:
area = 2 * PI * radius;
cout << "border is: " << 2*(l+w);
Order of Operations
In an expression with more than one operator, evaluate in
this order:
- (unary negation), in order, left to right
* / %, in order, left to right
+ -, in order, left to right
In the expression 2 + 2 * 2 – 2
Order of Operations
Associativity of Operators
• - (unary negation) associates right to left
• *, /, %, +, - associate right to left
• parentheses ( ) can be used to override the order of
operations:
2 + 2 * 2 – 2 = 4
(2 + 2) * 2 – 2 = 6
2 + 2 * (2 – 2) = 2
(2 + 2) * (2 – 2) = 0
Grouping with Parentheses
Algebraic Expressions
• Multiplication requires an operator:
Area=lw is written as Area = l * w;
• There is no exponentiation operator:
Area=s2 is written as Area = pow(s, 2);
• Parentheses may be needed to maintain order of operations:
is written as
m = (y2-y1) /(x2-x1);
Algebraic Expressions
15 mins
Write a complete C++ program that
gets x, y, z numbers from user to
Class
calculate the provided functions Exercise
below.
𝟐𝒙+𝟓𝒛 𝟓𝒙+𝟐𝒚
• 𝒇=
𝒙 𝒚+𝒛
4𝑦 2𝑧+2
• 𝑓= 3𝑥
3𝑥
2
+ 4𝑥𝑦𝑧
𝑦
3:3
Type Casting
Type Casting
• Used for manual data type conversion
• Useful for floating point division using ints: double m;
m = static_cast<double>(y2-y1)
/(x2-x1);
• Useful to see int value of a char variable:
char ch = 'C';
cout << ch << " is "
<< static_cast<int>(ch);
Type Casting in Program 3-9
C-Style and Prestandard Type Cast Expressions
• C-Style cast: data type name in ()
cout << ch << " is " << (int)ch;
sum = sum + 1;
Formatting Output
Formatting Output
• Can control how output displays for numeric, string data:
• size
• position
• number of digits
• Using cin with the >> operator to input strings can cause
problems:
• To work around this problem, you can use a C++ function named
getline.
Using getline in Program 3-19
Working with Characters and string Objects
• To read a single character:
• Use cin:
char ch;
cout << "Strike any key to continue";
cin >> ch;
Problem: will skip over blanks, tabs, <CR>
• Use cin.get():
cin.get(ch);
Will read the next character entered, even whitespace
Using cin.get() in Program 3-21
Working with Characters and string Objects
• Mixing cin >> and cin.get() in the same program
can cause input errors that are hard to detect
• To skip over unneeded characters that are still in the
keyboard buffer, use cin.ignore():
cin.ignore(); // skip next char
cin.ignore(10, '\n'); // skip the next
// 10 char. or until a '\n'
string Member Functions and Operators
• To find the length of a string:
string state = "Texas";
int size = state.length();
A Case Study
A Case Study
• General Crates, Inc. builds custom-designed wooden crates.
• You have been asked to write a program that calculates the:
• Volume (in cubic feet)
• Cost
• Customer price
• Profit of any crate GCI builds
Variables
Program Design
The program must perform the following general steps:
Step 1:
Ask the user to enter the dimensions of the crate
Step 2:
Calculate:
the crate’s volume
the cost of building the crate
the customer’s charge
the profit made
Step 3:
Display the data calculated in Step 2.
General Hierarchy Chart
Get Crate Dimensions
Calculate Volume, Cost, Customer
Charge, and Profit
Display Calculated Data
Psuedocode
1. Ask the user to input the crate's length.
2. Ask the user to input the crate's width.
3. Ask the user to input the crate's height.
4. Calculate the crate's volume.
5. Calculate the cost of building the crate.
6. Calculate the customer's charge for the crate.
7. Calculate the profit made from the crate.
8. Display the crate's volume.
9. Display the cost of building the crate.
10. Display the customer's charge for the crate.
11. Display the profit made from the crate.
Calculations
• The following formulas will be used to calculate the crate’s volume,
cost, charge, and profit: