lecture12 Input and Output

lecture12 Input and Output - 1 Janice Regan, CMPT 128, Jan...

Info iconThis preview shows pages 1–10. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 1 Janice Regan, CMPT 128, Jan 2007 CMPT 128: Introduction to Computing Science for Engineering Students More Input and Output Janice Regan, CMPT 128, Jan 2007 2 What is an object C++ is designed for object oriented programming An object is a thing in the problem your are solving Think of the stream of output from your program as an object. Includes the data you want to output Includes all the things you do to it (methods or functions) to output it Janice Regan, CMPT 128, Jan 2007 3 Displaying results on your screen Most basic I/O objects cout, cerr, endl All I/O objects are defined in the C++ library called iostream. To access the objects in the iostream library you must have the following pre-processor directive and statement at the beginning of your program file:i #include <iostream> //preprocessor directive //this directive makes the objects in the iostream //library available to your program using namespace std; //C++ statement: also necessary to make objects in libraries available Janice Regan, CMPT 128, Jan 2007 4 Streams Streams Special objects Deliver program input and output to/from your program Think of a stream of output, cout Think of a stream of input, cin Think of << as an operator to move data from your program to an ouput stream Think of >> as an operator to move data from an input stream into your program Janice Regan, CMPT 128, Jan 2007 5 Streams A stream can be thought of as a flow of characters An input stream flows into your program Can come from keyboard (cin) Can come from file An output stream flows out of your program Can go to screen (cout, or cerr) Can go to file Janice Regan, CMPT 128, Jan 2007 6 Reading/Writing data (1) Use iostream objects cin and cout cin >> num; Waits on-screen for keyboard entry Value entered at keyboard is placed in the memory location reserved for variable num, that is the entered value is "assigned" to num Janice Regan, CMPT 128, Jan 2007 7 Reading/Writing data (2) cout << num; Value from the memory location reserved for variable num, is displayed on the screen To remember direction of >> think of it as an arrow pointing to where the data is going cin >> num; //value is being put into variable num cout << num; // value in num is being sent to the screen (cout) Janice Regan, CMPT 128, Jan 2007 8 Error Output Output with cerr cerr works in the same way as cout Two different streams of information cout and cerr Both streams are displayed on the console (screen) Can separate the two streams Display error information to the screen Redirect output to a file Provides mechanism for distinguishing between regular output and error output. Janice Regan, CMPT 128, Jan 2007 9 Other Streams Can define other streams Stream can flow into a file Can have multiple streams each flowing into their own file Streams can flow out of files Can have multiple streams each flowing out of its...
View Full Document

Page1 / 46

lecture12 Input and Output - 1 Janice Regan, CMPT 128, Jan...

This preview shows document pages 1 - 10. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online