{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Notes5 - Chapter 5 I/O Streams Goals To determine how to...

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

View Full Document Right Arrow Icon
Chapter 5 Chapter 5 I/O Streams I/O Streams Goals: Goals:     To determine how to use external files for input and output To determine how to use external files for input and output     To investigate means by which file output can be formatted To investigate means by which file output can be formatted     To examine how to use I/O streams as function arguments To examine how to use I/O streams as function arguments     To introduce the concept of end-of-file for external input To introduce the concept of end-of-file for external input     To explore the nuances of character I/O with To explore the nuances of character I/O with get get & & put put
Background image of page 1

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

View Full Document Right Arrow Icon
Chapter 5 Chapter 5 CS 140 CS 140 Page  Page 2 #include <iostream> #include <fstream> using namespace std; bool userWantsGrosses(); void outputTotalGross( double gross); void main() { int dayCount = 0; double dailyGross; double totalGross = 0.0; ifstream fileContainingGrosses; if (userWantsGrosses()) { fileContainingGrosses.open("GrossFile.txt"); while (dayCount < 365) { dayCount++; fileContainingGrosses >> dailyGross; totalGross += dailyGross; } fileContainingGrosses.close(); outputTotalGross(totalGross); } else cout << "Well, why did you bother " << "executing this program, " << "you goof?" << endl << endl; return ; } Input Files When a program  When a program  needs to retrieve a  needs to retrieve a  stream of data  stream of data  from an external  from an external  source other than  source other than  the keyboard, it  the keyboard, it  may use an input  may use an input  file stream. file stream. This library enables the  This library enables the  use of external files. use of external files. The declaration of  The declaration of  an an ifstream ifstream variable. variable. Opening the input  Opening the input  file in the current  file in the current  directory, using the  directory, using the  filename filename GrossFile.txt GrossFile.txt . . Reading the  Reading the  next value from  next value from  the input file. the input file. Closing the input  Closing the input  file.  Note that the  file.  Note that the  file name is not  file name is not  specified for closing. specified for closing.
Background image of page 2
Chapter 5 Chapter 5 CS 140 CS 140 Page  Page 3 bool userWantsGrosses() { char yOrN; cout << "Would you like to compute the " << “year\'s grosses? (Enter Y or N) "; cin >> yOrN; while ((yOrN != 'y') && (yOrN != 'Y') && (yOrN != 'n') && (yOrN != 'N')) { cout << "Your response must be the " << "letter \'Y\' or the letter \'N\'." << " Please try again: "; cin >> yOrN; } return ((yOrN == 'y') || (yOrN == 'Y')); } void outputTotalGross( double gross) { cout.setf(ios::fixed); cout.setf(ios::showpoint); cout.precision(2); cout << "The total gross is $" << gross << endl << endl; } 804.89 692.45 614.39 760.26 541.53 229.55 954.82 206.73 730.13 685.33 405.49 351.50 152.51 606.81 198.79 127.55 869.49 382.64 605.27 263.36 124.58 544.82 291.35 822.71 987.64 550.15 537.19 363.20 141.80 432.30 843.20 229.25 766.33 964.90 803.19 845.54 819.56 226.56 171.96 726.15 173.52 207.94 306.04 365.12 305.00 652.27 705.38 124.09 394.89 969.18 133.97 582.64 439.77 912.91 547.71 767.07 828.21 165.28 807.91 922.96 885.58 114.42 907.91 747.27 239.85 769.38 991.46 645.89 112.00 601.68 590.91 531.67 283.88 354.00 363.00 129.22 546.80 607.22 950.95 701.57 583.82 445.84 350.40 464.60 788.99 512.66 607.69 704.70 676.68 693.55 928.43 465.61 984.07 222.49 624.66 419.50 719.75 347.79 678.47 988.55 662.02 250.32 350.43 932.13 959.49 753.67 110.79 365.61 737.57 217.85 121.83 926.01 863.44 756.00 149.19 750.56 673.25 166.11 930.27 383.58 614.17 316.75 278.47 177.01
Background image of page 3

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

View Full Document Right Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}