Presentation is loading. Please wait.

Presentation is loading. Please wait.

CMPSC 121- Spring 2015 Lecture 6 January 23, 2015.

Similar presentations


Presentation on theme: "CMPSC 121- Spring 2015 Lecture 6 January 23, 2015."— Presentation transcript:

1 CMPSC 121- Spring 2015 Lecture 6 January 23, 2015

2 Input  C++ utilizes the cin object and extraction operator to read data from keyboard and store in memory locations. cin>> variablename;  Variable must be declared before it can be used.  Input stops when white space is encountered.  Can enter multiple values by separated by extraction operators. cin>>x>>y; //data is stored in order given  Usually users are prompted by a cout statement to enter data.

3 Examples cout > radius; // must be declared prior cout > length >>width; // must be declared prior ----or---- cin>>length; cin >>width;

4 Caution  cin does not confirm data type.  Given the following code segment int alpha; cout >alpha; What would happen if the user entered a floating point number or a character? (See also pg 87 in book)

5 Input for char Variable  Remember that a char variable only stores one character (letter, digit, or special character).  Using cin will allow the extraction of 1 character from the input stream.  White spaces are skipped.  Extra characters remain in the input stream.

6 Input of C-strings  Remember a C-string is an array of characters and may be declared as char varname[n]; char last[12];  You can use cin to store characters in this C- string. cin>>last;  Input will stop is a white space is encountered.  There is no check for number of characters.

7 Input of String Objects  Remember to declare a variable of the class string, you must include the string library file. # include string first;  You can use cin to store characters in this string object cin>>first;  Input will stop is a white space is encountered.  String objects do not have a preset length.

8 Questions ???

9 Data Overflow and Underflow   C++ does not check for data being within range of data type.   How the data is stored may be compiler dependent.   In many compilers, the number will “wrap”. For example given the following statements short x = 32767; x = x + 5; cout<< x<<endl; will usually output -32764.

10 Data Type Coercion   Automatic conversion of a data type for mathematical operations or assignment. int alpha = 10.5; // 10.5 is coerced to 10 double beta = 4; // 4 is coerced to 4.0 alpha = alpha/beta; //two coercions occur //10 is coerced to 10.0 for the division //the value 2.5 is truncated to 2 for assignment. beta = alpha * 4.4; //what happens to alpha, beta?

11 Type Casting   Code specifies the conversion of the data type for the purposes of an operation.   Three methods: static_cast (variable or expression) data type (variable or expression) (data type) variable or expression The latter two are considered to be C-style   Type casting a variable will not change what is stored in a variable

12 Examples   Consider the following statements: int a = 6, b = 9; double c; c = static_cast (b)/a; // the value stored in b, 9, is converted to 9.0 // before the division, but the integer value 9 is // still stored in b; cout<<a<<“ ”<< b <<“ ”<<c<<endl; //what is output?   Would c = static_cast (b/a); produce the same results.

13 Remember   There are only 2 methods to change what is stored in a variable.   Assignment statement   Input new value

14 Named Constants   Named, or symbolic, constants allow the user to assign a value that will not change to a memory location   Has the format of const data type identifier = value; const double PI = 3.14159;   Like variables, named constants must be declared before they are used (usually before main).   A value must be assigned at time of declaration.

15 Named Constants   Utilizes space in main memory, however the value in memory space cannot be changed unless code is changed. PI = 4.234; // would be a syntax error   Makes updates to the program easier if a value is to be used over and over again

16 Define   Utilizes a preprocessor directive to associate a value with a word.   Has the format #define identifier value #define tax_rate 4.5 the data type is not needed because no memory space is allocated.   No memory space is used. During compilation, the word is changed to the value.

17 Questions

18 Compound Operators  Provide a shortcut for assignment statements.  Combines the = operator with another arithmetic operator a + = 5.67; is equivalent to a = a + 5.67; a + = 5.67; is equivalent to a = a + 5.67; a -= 6.0; is equivalent to a = a – 6.0; a *= 3.4; is equivalent to a = a * 3.4; a /= 2.1; is equivalent to a = a /2.1; a %= 3; is equivalent to a = a % 3;  Reminder---the % operator only works for integer data types.

19 Examples  Suppose we had the following declarations: double alpha = 6.0, beta = 7.5; int gamma = 8;  What would be the result of the following?  alpha += 4;  gamma *= 4;  beta -= 4.5;  gamma /= 6.0;  beta %= 2;

20 Questions


Download ppt "CMPSC 121- Spring 2015 Lecture 6 January 23, 2015."

Similar presentations


Ads by Google