CPS120: Introduction to Computer Science - PowerPoint PPT Presentation

About This Presentation
Title:

CPS120: Introduction to Computer Science

Description:

CPS120: Introduction to Computer Science Screen I/O Lecture 11 ... – PowerPoint PPT presentation

Number of Views:202
Avg rating:3.0/5.0
Slides: 19
Provided by: wcc94
Learn more at: http://space.wccnet.edu
Category:

less

Transcript and Presenter's Notes

Title: CPS120: Introduction to Computer Science


1
CPS120 Introduction to Computer Science
  • Screen I/O
  • Lecture 11

2
Console I/O
  • console I/O refers to using the keyboard and
    screen as the standard input and output devices,
    respectively.

3
C-In (cin)
  • cin is a stream of data flowing from an input
    device such as the keyboard (considered to be the
    standard input device) into your program.

4
Input Operations
  • The operator gtgt is known as the input operator.
    It is also known as the extraction operator
  • You use the input operator in statements like,
  • cin gtgt numItems
  • which would allow the user to input a value to
    be stored in the variable numItems.

5
Multiple Input
  • cin to allows the user to input several items
    with one C statement. The user however must
    type one or more spaces between each separate
    inputted value
  • Example
  • cin gtgt value1 gtgt value2 gtgt value3
  • Inputs need to have spaces or tabs between them
    (cant use a comma to delimit C)
  • However, this prevents you from properly giving
    the user individual prompt messages

6
Inputting Words
  • gtgt can be used but once it hits a space or a tab,
    the rest of the string is ignored
  • Use the get function for strings that contain
    white space
  • String ends when you press enter

7
Complexities of Word Input
  • Some things are done automatically with gtgt
  • get does not skip over line breaks and spaces
  • If the user enters a string longer than the
    length specified in the call to the get function,
    the remaining characters are left in the input
    stream
  • Get always ignores the new line character (\n)
    and leaves it in the stream
  • Use the ignore function to flush the contents of
    the input stream
  • cin.ignore(80, \n)

8
Output Operations
  • The operator ltlt is known as the output operator.
    It is also known as the insertion operator
  • You use the output operator in statements
    like cout ltlt "Hello world"

9
C-Out (cout)
  • cout is a stream which represents data flowing
    from one place to another.
  • The statement
  • cout ltlt "Hello world"
  • causes data to flow from your program to the
    screen.
  • The stream, cout, leads to what your computer
    considers to be its standard output device
    (usually the screen)

10
Line Spacing
  • In order to end a line in an output statement you
    may use the new line character, \n, instead of
    endl.
  • Examples
  • cout ltlt "Hello world" ltlt '\n'
  • cout ltlt "Hello world" ltlt "\n"
  • cout ltlt "Hello world\n"
  • These are practically equivalent to
  • cout ltlt "Hello world" ltlt endl

11
Escape Sequences
  • Other useful "escape sequences" (since the \ is
    the escape operator) are
  • \t to generate a tab
  • \\ to print a backslash
  • \' to print a single quote
  • \" to print a double quote

12
Using setf and unsetf
  • Each stream has format options that can be
    changed
  • OPTION DESCRIPTION
  • left Left-justifies the output
  • right Right-justifies the output
  • showpoint Displays decimal point and trailing
    zeros for floats
  • uppercase Displays e in scientific as E
  • showpos Displays a leading plus sign
  • scientific Displays floating point number
    scientifically
  • fixed Displays floating-point in normal notation

13
Using Format Options
  • Format options are set immediately prior to the
    COUT statement
  • float x 24.0
  • cout ltlt x ltlt \n // displays 24
  • cout.setf(iosshowpoint)
  • cout ltlt x ltlt \n // displays 24.00000
  • cout.unsetf(iosshowpoint)
  • cout ltlt x ltlt \n // displays 24

14
Using Manipulators
  • You must include the ltiomanip.hgt header file at
    the top of your program in order to use the
    setprecision, setw, and other manipulators. You
    must use place the following compiler directive
    at the top of your program.include ltiomanip.hgt
  • I/O manipulators are placed directly in the
    output statement
  • cout ltlt setprecision(2) ltlt price ltlt \n

15
Setting Precision
  • The setprecision manipulator allows you to limit
    the number of digits that are displayed when a
    numeric data type is displayed
  • cout ltlt setprecision(2) ltlt price ltlt '\n'
  • only allows the leading two digits of the value
    stored in the variable, price, to be displayed

16
More Precisely
  • If the fixed format was set previously with the
    statement
  • cout.setf(iosfixed)
  • then the setprecision(2) manipulator would have
    the effect of rounding or truncating price (and
    all future floating-point values in the cout
    stream) to the hundredths place

17
Field Width
  • The setw manipulator controls the width of the
    field when displaying a value. The statement
  • cout ltlt setw(10) ltlt umEndow ltlt endl
  • sets the width of the field allocated for the
    variable, umEndow, to 10 characters

18
Formatted Output
  • cout.setf(ios fixed)
  • Print fixed point form, not in exponential form
  • cout.setf(ios showpoint)
  • Says to always print the decimal point
  • cout.precison(2)
  • Says to print out the two most significant
    decimal digits, after rounding to this precision
Write a Comment
User Comments (0)
About PowerShow.com