Source of cppstr6.cpp


  1: // Filename: CPPSTR6.CPP
  2: // Purpose:  Reads and displays the lines from a textfile, pausing
  3: //           whenever a line of 80 dollar signs ($) is encountered.

  5: #include <iostream>
  6: #include <fstream>
  7: #include <string>
  8: using namespace std;

 10: #include "PAUSE.H"

 12: int main()
 13: {
 14:     cout << "\nThis program asks the user for the name "
 15:          << "of a textfile and then displays the "        << endl
 16:          << "lines from that file, pausing whenever "
 17:          << "it sees a line of 80 dollar signs ($).  "    << endl
 18:          << "Note that any such line of $ symbols "
 19:          << "is also actually displayed in the output. "  << endl
 20:          << "Since all lines from the files are read "
 21:          << "in before being displayed, there is a "      << endl
 22:          << "maximum number of lines (20) that the "
 23:          << "file can contain. "                          << endl << endl;

 25:     const int MAX_LINES = 20;
 26:     typedef string FileContentsType[MAX_LINES];

 28:     ifstream inFile;
 29:     string fileName, line;
 30:     FileContentsType text;
 31:     int lineIndex = 0;

 33:     cout << "Enter name of file to display: ";
 34:     cin >> fileName;  cin.ignore(80, '\n');  cout << endl;

 36:     inFile.open(fileName.c_str());
 37:     getline(inFile, line);
 38:     while (inFile)
 39:     {
 40:         text[lineIndex] = line;
 41:         getline(inFile, line);
 42:         if (inFile) lineIndex++;
 43:     }
 44:     inFile.close();

 46:     const string DOLLARS(80, '$'); // Creates a string of 80 dollar signs
 47:     cout << "Here are the lines in the file " << fileName << ":\n";
 48:     for (int i = 0; i <= lineIndex; i++)
 49:     {
 50:         cout << text[i] << endl;
 51:         if (text[i] == DOLLARS) Pause(0);
 52:     }
 53:     Pause(0);  cout << endl;

 55:     return 0;
 56: }