Chapter 3' Expressions and Interactivity - PowerPoint PPT Presentation

1 / 14
About This Presentation
Title:

Chapter 3' Expressions and Interactivity

Description:

The C system matches the variable with the correct data type in the ... tick backslash n tick. You can store the newline character in a character data item. ... – PowerPoint PPT presentation

Number of Views:31
Avg rating:3.0/5.0
Slides: 15
Provided by: cathe67
Category:

less

Transcript and Presenter's Notes

Title: Chapter 3' Expressions and Interactivity


1
Chapter 3. Expressions and Interactivity
2
PROGRAM INPUT
Input Stream and the Extraction Operator
gtgt include ltiostreamgt General Format cin gtgt
variable gtgt variable int cost //declare int
data type cin gtgt cost // get data and store in
cost The C system matches the variable with
the correct data type in the input buffer, if
possible.
3
THE READING MARKER
  • The Reading Marker indicates the next data item
    to be read.
  • The extraction operator leaves the reading marker
    positioned after the last data item read.
  • The NEWLINE character is created when the RETURN
    or ENTER key is depressed.
  • The endl manipulator also outputs a newline.
  • The newline may be created in code this way
    \n
  • tick backslash n tick
  • You can store the newline character in a
    character data item.

4
Entering Multiple Values
  • The cin object may be used to gather multiple
    values at once.

5
Reading Strings
  • cin can read strings as well as numbers.
  • Strings are stored in character arrays.
  • char Name10
  • Name has 9 bytes for data, 1 byte for the Null
    delimiter

6
MATHEMATICAL EXPRESSIONS
rvalue expression is assigned lvalue
expression // ONE equal sign is the assignment
operator sum 12 4 sum 10
7
PRECEDENCE AND ASSOCIATIVITY
  • PRECEDENCE defines the priority with which each
    operator in an expression will be applied.
  • ASSOICATIVITY defines the order of evaluation of
    operators in an expression, containing operators
    of the same precedence. If the evaluation is
    from left to right, the operator is termed
    left-to-right associative. Otherwise, the
    operator is right-to-left associative.
  • EXAMPLE
  • 4 - 2 is evaluated (5 4) - 2
  • a b c is evaluated (a b ) c
  • X y 2 is evaluated X ( y 2 )

8
Type Coercion or Widening or Promotion long
double double float unsigned long long unsigned
int int char
9
EXPLICIT COERCION or WIDENING or PROMOTION The
programmer can explicitly change the data type
for the instance of the computation or
assignment. Float(someint) somefloat 4.8
float(someInt 3) // result is a float Example
int sum 60 int count 80 float average
0.0 / Wrong / average sum/count //
result is 0.0 / Right!!! / average
static_castltfloatgtsum / static_castltfloatgtcount
average (float)sum/(float)count / either
result will be 0.75 /
10
The define Preprocessor Directive
  • define PI 3.14159 // no semicolon, please!
  • is roughly the same as
  • const float PI3.14159 // always a float!
  • PI is always a float in the second statement.

11
Formatting Output With Member Functions
  • cout.width(11) //calls the width member
    function,
  • //same as setw(11)
  • cout.precision(2) //sets precision to 2
    significant digits
  • cout.setf(iosfixed) //same as
    setiosflags(iosfixed)
  • --OR--
  • //To display fixed point, decimal point and
    left-justify
  • cout ltlt setprecision(2) ltlt fixed ltlt showpoint ltlt
    endl
  • // To disable flags
  • cout ltlt right

12
  • FORMATTING INPUT
  • Three ways to format input
  • char Name10
  • cin.get(Name,10)
  • char Name10
  • cin gtgt setw(10) gtgt Name
  • char Name10
  • cin.width(10)
  • cin gtgt Name

13
Char String5 100 101 102 103
104 cin.width(5) cin gtgt String Morris\n
Reading Marker There is now
garbage in the input buffer.
100 101 102 103 104 M o r r \0
14
Reading a Line of Input
  • cin.getline(Name, 15)
  • Only 14 characters will be read.
Write a Comment
User Comments (0)
About PowerShow.com