1/16
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced | Call with Kai | Chat |
|---|
No analytics yet
Send a link to your students to track their progress
The cin Object
Standard input object
requires iostream file
Used to read input from keyboard
Information retrieved from cin with »
Input stored in one more more variables
cin converts data to the type that matches the variable
Can be used to input more than one value: cin»height»width;
Multiple values from keyboard must be separated by spaces
Order is important: first value entered goes to first variable
Displaying a Prompt
A prompt is a message that instructs the user to enter data
ALWAYS use cout to display a prompt before each cin statement
Mathematical expressions
Can create complex expressions using multiple mathematical operators
An expression can be literal, a variable, or a mathematical combination of constants and variables
Can be used in assignment, cout, or another statement: area = 2*PI*radius;cout«“border is:” « 2*(l+w);
- (unary negation) associates right to left
*,/,%,+,- all associate right to left
parentheses () can be used to override the order of operations
Multiplication requires an operator: Area = l * w;
There is no exponentiation operator: Area= pow(s,2);
Parentheses may be need to maintain order of operations: m = (y2-y1) / (x2-X1) ;
Type Conversion
Operations are performed between operands of the same type
If not of the same type, C++ will convert one to the type of the other
This can impact the results of the calculations
Ranked from largest number they can hold to lowest, types are: long double, double, float, unsigned long, long, unsigned int, int
Type Coercion: the automatic conversion of an operand to another data type
Promotion: Convert to a higher type
Demotion: Convert to a lower type
1. char, short, unsigned short will automatically be promoted to int
2. When operating on values of different data types, the lower one is promoted to the type of the higher one
3. When using the = operator, the type of expression on the right will be converted to the type of variable on the left
Overflow occurs when assigning a value that is too large to be held in a variable
Underflow occurs when assigning a value that is too small to be held in a variable
Different systems may display an error, stop the program, or continue execution using the incorrect value!!
Type Casting
Used for manual data type conversion
Useful for floating point division using ints
Useful to see int value of a char variable
C-Style and Prestandard Type Cast Expressions
C-Style cast: data type name in (): cout«ch«”is”«(int)ch;
Prestandard C++ cast: value in (): cout«ch«”is”«int(ch);
Both are still supported in C++, although static_cast is preferred
Multiple Assignment
The = can be used to assign a value to multiple variables: x=y=z=5;
Value of = is the value that is assigned
Associates right to left (x=(y=(z=5));
Combined Assignment
Sum = sum+1; adds one to variable sum
Combined assignment operators provide shorthand for such statements, sum= sum+1; is equivalent to sum+=1;
Combined assignment operators are +=, -=, *=, /=, %=
Formatting Output
Can control how output displays for numeric, string data:
Size
Position
Number of digits
Requires iomanip header file
Stream Manipulators
Used to control how an output field is displayed
Some affect just the next value displayed
setw(x): print in a field at least x spaces wide. Use more spaces is not wide enough
Some affect values until changed again
fixed: use decimal notation for floating-point values
setprecision(x): when used with fixed, print floating-point value using x digits after the decimal. Without fixed, print floating point value using x significant digits
showpoint: always print decimal for floating-point values
Working with Characters and string Objects
Using cin with the » operator to input strings can cause problems
It passes over ant ignores any leading whitespace characters (spaces, tabs, or line breaks)
To work around this problem, you can use a C++ function named getline
To read a single character:
Use cin: char ch; cout « “Strike any key to continue”; cin»ch;
PROBLEM: Will skip over blanks, tables, <CR>
Use cin.get(ch);
Will read next character entered, even whitespace
Mixing cin » and cin.get() in the same program can cause input errors that are hard to detect
To skip over unneeeded characters that are still in the keyboard buffer use cin.ignore()
string Member Functions and Operators
To find the length of a string: string state = “Texas”; in size = state.length();
To concatenate (join) multiple strings: greeting2=greeting1 + name1;
Or using the += combined assignment operator: greeting1 += name2;
Basic Mathematical Library Functions
Require cmath header file
Take double as input, return a double
Commonly used functions: sin, cos, tan, sqrt, log, abs
Additional Mathematical Library Functions
Require cstdlib header file
rand (): returns a random number (int) between 0 and the largest int the compute holds. Yields the dame sequence of numbers each time the program is run
srand(x): initializes random number generator with unsigned int x
Uses of random numbers
Games, simulations, statistical analysis, data encryption
Generating Random Numbers
use # include <random>
Create a random number engine to generate a random sequence of bits and a distribution object to format the bits into numbers of a specific data type within a specified range
Hand Tracing a Program
Acting as if you are the computer, executing a program
‘Execute’ each statement one by one
Record the contents of variables after statement execution, using a hand trace chart (table)
Useful to locate logic or mathematical errors