Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 11: Handling Real-World Data Entry."— Presentation transcript:

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

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

3 Topic Pre-Processor Directives

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 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 pre-processor step compilation step compilation step link step link step #include allows the use of external code within our programs #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. The #define directive is used to assign a value to a symbolic name. Generally placed outside any functions Generally placed outside any functions With the way a #define is used, the scope usually propagates throughout the program With the way a #define is used, the scope usually propagates throughout the program Most often used for global constants Most often used for global constants The const declaration is preferred, except for special purposes The const declaration is preferred, except for special purposes

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

7 Topic Reading Input and Writing Output

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

9 Other Functions The cout object allows you to put data one character at a time: 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: 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: 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 eof() function returns true after the EOF character is read by get() The extraction operator generally ignores whitespace The extraction operator generally ignores whitespace The get() function does not 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 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 Imagine using the cin.get() function to parse every character

12 Topic Non-Standard I/O Streams

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

14 Example Go back to the fundraiser assignment Go back to the fundraiser assignmentfundraiser assignmentfundraiser assignment Did you find it painful to enter values for testing? Did you find it painful to enter values for testing? Come up with a set of values we can use for that purpose and save it in a file Come up with a set of values we can use for that purpose and save it in a file Modify your program so that it will open the file, read from it, then write to another Modify your program so that it will open the file, read from it, then write to another FundContribs.txt FundContribs.txt FundResults.txt FundResults.txt

15 Topic Formatting Output

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

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


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

Similar presentations


Ads by Google