Chap3 slides [Compatibility Mode] Programming Fundamentals with C++ 1 Chapter 3 COMPLETING THE BASICS Programming Fundamentals with C++ 2 Chapter 3 n Assignment operator n Formatting the output n Usin[.]
Chapter COMPLETING THE BASICS Programming Fundamentals with C++ Chapter n n n n n Assignment operator Formatting the output Using mathematical library functions Program input using the cin object String functions Programming Fundamentals with C++ Overview n In the last chapter, we studied how result are displayed and how numerical data are stored and processed using variables and assignment statements n In this chapter, we study some C++’s additional processing and input capabilities Programming Fundamentals with C++ Assignment n How we place data items into variables? • Read in values typed at the keyboard by the user • Use an assignment statement n Assignment statement examples: length = 25; cMyCar = “Mercedes”; sum = + 7; newtotal = 18.3*amount; slope = (y2 – y1)/(x2 – x1) Programming Fundamentals with C++ Assignment statement n Assignment operator (=) are used for assignment a value to a variable and for performing computations n Assignment statement has the syntax: variable = expression; n Expression is any combination of constants, variables, and function calls that can be evaluated to yield a result Programming Fundamentals with C++ Assignment statement (cont.) n The order of events when the computer executes an assignment statement is - Evaluate the expression on the right hand side of the assignment operator - Store the resultant value of the expression in the variable on the left hand side of the assignment operator n Note: The equal sign here does not have the same meaning as an equal sign in mathematics Each time a new value is stored in a variable, the old one is overwritten Programming Fundamentals with C++ Example n This program calculates the volume of a cylinder, given its radius and height #include int main() { float radius, height, volume; radius = 2.5; height = 16.0; volume = 3.1416 * radius * radius * height; cout