1 / 16

Bellevue University

CIS 205: Introduction to Programming Using C++ Lecture 11: Handling Real-World Data Entry. Bellevue University. In This Lesson We Will:. Use pre-processor #defines to declare global constants Read data from a file Write data to a file

ursa-cook
Download Presentation

Bellevue University

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CIS 205: Introduction to Programming Using C++ Lecture 11: Handling Real-World Data Entry Bellevue University

  2. In This Lesson We Will: Use pre-processor #defines to declare global constants Read data from a file Write data to a file Address the formatting of input and output with respect to I/O streams

  3. Pre-Processor Directives Topic

  4. The Pre-Processor • Recall our discussion of the way the C++ compiler works: though we often treat it as a single operation, there are discreet steps within the process • pre-processor step • compilation step • link step • #include allows the use of external code within our programs

  5. #define The #define directive is used to assign a value to a symbolic name. Generally placed outside any functions With the way a #define is used, the scope usually propagates throughout the program Most often used for global constants The const declaration is preferred, except for special purposes

  6. Special Purposes One candidate is for system-specific information Another is for controlling the inclusion of files in compilation We will see some of the former today We will see the latter when we begin dividing our code across multiple files

  7. Reading Input and Writing Output Topic

  8. Standard Objects The iostream library defines objects which may be used for console I/O cout for output cin for input Familiar operators: << is the insertion operator >> is the extraction operator The endl object starts a new line Objects defined by the system

  9. Other Functions The cout object allows you to put data one character at a time: cout.put(aChar); The cin object allows you to get data one character at a time: cin.get(aChar); Also offers a special (Boolean) function to test for the end of a file: cin.eof();

  10. Worth Noting The eof() function returns true after the EOF character is read by get() The extraction operator generally ignores whitespace The get() function does not

  11. Food For Thought • This class has de-emphasized input filtering; generally we expect that the extraction operator will give valid data • Imagine using the cin.get() function to parse every character

  12. Non-Standard I/O Streams Topic

  13. File I/O The standard I/O objects represent the terminal and keyboard Also may want to use files for I/O Represent the files as stream objects similar to cout and cin Access file streams using the fstream library

  14. Formatting Output Topic

  15. Console Formatting • GUI interfaces have changed the way formatting is handled in the real world • However, here are some formatting operators provided by the iomanip library • setw(int n) • setprecision(int n) • left • right

  16. In This Lesson We Have: • Used pre-processor #defines to declare global constants • Read data from a file • Written data to a file • Addressed the formatting of input and output with respect to I/O streams

More Related