Thursday, June 22, 2017

Programming Challenge 12.5 - Line Numbers

Example Files: long.txt
                          short.txt


/* Line Numbers - This is a modification of Programming Challenge 12.2
   The program asks the user for the name of a file. The program displays
    the contents of the file on the screen. Each line of screen output is
    preceded with a line number, followed by a colon. The line numbering
    starts at 1. If the file's contents won't fit on a single screen, the
    program displays 24 lines of output at a time, and then pauses. Each
    time the program pauses, it waits for the user to strike a key before
    the next 24 lines are displayed. */

#include "Utility.h"

void openFile();
bool isGood(fstream &, const string);
void displayText(const vector<string>);

int main()
{
    openFile();

   pauseSystem();
   return 0;
}

/* **********************************************************
   Definition: openFile

    This function reads in and stores the contents of a text
    file in a vector of string objects.
   ********************************************************** */

void openFile()
{
    string  fileName = " ";            /* To hold the file name */
    string  tmpText = " ";            /* To hold the text         */  
    fstream textFile;                    /* File stream object     */

    vector<string> gamesText;        /* Vector of string objects to hold
                                               the file contents */

    cout << "\n\tLINE NUMBERS\n\n"
         << "\tEnter the name of the file you wish to open: ";
    cin >> fileName;

    if (isGood(textFile, fileName))
    {
        while (getline(textFile, tmpText))
        {
            gamesText.push_back(tmpText);
        }
        textFile.close();

        displayText(gamesText);
    }
    else
    {
        cout << "\tERROR: Cannot open the file.\n"
              << "\tPress Enter or click [X] to exit ...\n";
    }
}

/* **********************************************************
   Definition: isGood

    This function accepts a reference to an fstream object as
    argument. The file is opened for input. The function
    returns true upon success, false upon failure.
   ********************************************************** */

bool isGood(fstream &textFile, const string fileName)
{
    textFile.open(fileName, ios::in);

    if (!textFile.fail())
    {
        return true;
    }
    else
    {
        return false;
    }
}

/* **********************************************************
   Definition: displayText

    This function accepts a vector of string objects as its
    argument. It displays the contents of the string object.
    After every 24 lines of text being output to screen, the
    user is asked to press Enter to continue. If the remaining
    number of lines is less then 24, they are displayed. Else
    the process repeats, until the end of text is reached.
   ********************************************************** */

void displayText(const vector<string> gamesText)
{
    int numLines = 0;

    cout << "\n";
    cin.ignore();

    while (numLines < gamesText.size())
    {
        numLines++;
      
        cout << "\t" << setw(3) << right << numLines << ": "
              << gamesText[numLines-1] << setw(6) << right << "\n";

        if (numLines % 24 == 0)
        {
            cout << "\n\tPress Enter to continue:";
            cin.get();
            cout << "\n";
        }
    }

    if (gamesText.size())
    {
        cout << "\n\tEnd of text.\n"
              << "\tPress Enter to exit this program.";
    }
}

Example Output:






No comments:

Post a Comment