InputOutput - PowerPoint PPT Presentation

1 / 13
About This Presentation
Title:

InputOutput

Description:

No I/O is built into C . C allows generalized Input/Output through a mechanism ... Special characters have a backslash () followed by a printable character ... – PowerPoint PPT presentation

Number of Views:48
Avg rating:3.0/5.0
Slides: 14
Provided by: bridges
Category:

less

Transcript and Presenter's Notes

Title: InputOutput


1
Input/Output
  • Reading Chapter 3

2
C streams
  • No I/O is built into C
  • C allows generalized Input/Output through a
    mechanism called a stream

3
C streams
  • The C I/O library defines
  • Input stream (istream) sequence of characters
    coming into a program from an input device
  • Output stream (ostream) sequence of characters
    going from a program to an output device

4
C streams
  • To use stream I/O, program should have
  • include ltiostreamgt
  • using namespace std
  • The ltiostreamgt defines
  • An input stream (istream) object named cin
  • Pronounced see-in
  • Associated with the standard input device
    (keyboard)
  • An output stream (ostream) object named cout
  • Pronounced see-out
  • Associated with the standard output device
    (screen)

5
Extraction operator
  • The stream extraction operator gtgt extracts input
    from the input stream and stores it in a
    variable.
  • Example
  • cin gtgt age
  • The gtgt is a binary operator
  • Also known as get from
  • The left operand is an input stream object, such
    as cin
  • The right operand is a variable of simple type
  • Has left-to-right associativity

6
Extraction operator (Cont..)
  • The gtgt operator can be used more than one time in
    the same statement
  • cin gtgt age gtgt weight
  • Input values are separated by whitespaces
    (blanks, tabs, newlines)
  • The gtgt operator skips over (actually reads but
    does not store anywhere) leading white space
    characters as it reads your data from the input
    stream

7
Example
  • include ltiostreamgt
  • using namespace std
  • int main()
  • int someInt
  • float someFloat
  • cout ltlt "Enter an integer followed by a float
    "
  • cin gtgt someInt gtgt someFloat
  • cout ltlt "Integer " ltlt someInt ltlt endl
  • ltlt "Float " ltlt someFloat ltlt endl
  • return 0

8
Output
Enter an integer followed by a float 34
5.65 Integer 34 Float 5.65
9
Insertion Operator
  • The stream insertion operator ltlt inserts data
    into the output stream.
  • Example
  • cout ltlt 34
  • The ltlt is a binary operator
  • Also known as put to
  • The left operand is an output stream object, such
    as cout
  • The right operand is an expression of simple type
    or a string constant
  • Has left-to-right associativity

10
Insertion operator (Cont..)
  • The ltlt operator can be used more than one time in
    the same statement
  • cout ltlt "The answer is" ltlt 34
  • Output is stored in a buffer until
  • the buffer becomes full
  • the program terminates
  • an endl is inserted in the output stream

11
The endl manipulator
  • Used to terminate the current output line
  • Used to create blank lines in output
  • Example
  • cout ltlt "The answer is" ltlt 34 ltlt endl

12
Special ASCII characters
  • Special characters have a backslash (\) followed
    by a printable character
  • Can be used with ltlt operator

\0 Null \a Audible alarm \b Backspace \n New line
\r Carriage Return \t Horizontal
tab \" Quote \\ Backslash
13
Class Assignment
  • You have to write a C program to calculate the
    salary of a worker. The salary is calculated by
    multiplying the number of hours he has worked
    with the hourly wage. Ask the user to enter
    values for the number of hours and the hourly
    wage. Finally display the salary with 2 digits
    after the decimal points.
Write a Comment
User Comments (0)
About PowerShow.com