Numeric Types, Expressions, and Output ROBERT REAVES
Compound Arithmetic Expressions Arithmetic expressions can be made up of many constants, variables, operators, and parentheses. Precedence Level (highest lowest) Unary +, Unary - *, /, % +, - (…) are always evaluated first. Ex. 10 / 2 * 3 = 15 10 % 3 – 4 / 2 = -1 5.0 * 2.0 / 4.0 * 2.0 = 5.0 5.0 * 2.0 / (4.0 * 2.0) =1.25
Type Coercion and Type Casting Type Coercion is the implicit (automatic) conversion of a value from one data type to another. Ex. float someFloat = 12; Computer inserts extra machine language instructions to convert to Type Casting is the explicit conversion of a value from one data type to another, also called type conversion. A C++ cast operation consists of a data type name and then, within parentheses, the expression to be converted. Ex. float someFloat = float(3 * someInt * 2);
Arithmetic Expressions Possible to mix data types within an expression, this is called mixed type expression or mixed mode expression. Whenever an integer value and floating-point value are joined by an operator, implicit type coercion occurs: Integer value is temporarily coerced to a floating-point value Operation is performed Result is a floating-point value
What is a value-returning function? A function that returns a single value to its caller and is invoked from within an expression.
Void Functions void myFunc(...) {. } Notice how it begins with the word void instead of a data type like int or float. Void Function( (procedure) is a function that does not return a function value to its caller and is invoked as a separate statement. What do you mean by a separate statement?
Formatting Output What this means is to control how output appears visually on the screen or on a printout. The C++ standard library supplies many manipulators, but we will look at only five of them: endl setw fixed showpoint setprecision
Header Files endl, fixed, and showpoint are including inside the iostream header file to perform I/O. #include setw and setprecision are inside the iomanip header file. #include
Setw setw means “set width”, it lets us control how many character positions the next data item should occupy when it is output. Cout << setw(4) << “Hi” << endl; Output (_ means blank) _ _ Hi If number of characters to output is less then that amount of characters in the output the field will automatically expand to fit the output.
Fixed What happens when we use floating-point values and setw? Take the value 4.85, it takes four output positions to print this to the screen. Another problem with float-point values is that large values are printed in Scientific notation. may print as e+08 on some systems. Can use fixed to force all subsequent floating-point output to appear in decimal form instead of scientific notation. cout << fixed << 3.8 * x; However, if the number is whole number it will not print as a floating- point number.
Showpoint showpoint forces decimal points to be displayed in subsequent floating-point output, even for whole numbers. cout << showpoint << someFloat; What if we want just two decimal places?
setprecision setprecision specifies the desired number of decimal places. REMAINS IN EFFECT for all subsequent output. Should use setprecision and the fixed together to get correct results.
Additional String Operations .at() Included instead to the cctype header file; #include toupper() tolower()
At function At function allows for individual character access in a string. str1.at(pos); pos being the character position you want to access. This returns the character at that location within the string. string str1 = “Robert”; char letter = str1.at(0); The variable letter now is holding the character ‘R’;
toupper toupper(ch) returns the uppercase equivalent of ch, if ch is a lowercase character; ch, otherwise. Ex. toupper(‘r’) returns ‘R’ toupper(‘R’) returns ‘R’
tolower tolower(ch) returns the lowercase equivalent of ch, if ch is an uppercase letter; ch, otherwise. Ex. tolower(‘R’) returns ‘r’ tolower(‘r’) returns ‘r’