160 likes | 263 Views
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
E N D
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 Address the formatting of input and output with respect to I/O streams
Pre-Processor Directives Topic
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
#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
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
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
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();
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
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
Non-Standard I/O Streams Topic
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
Formatting Output Topic
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
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