Tags: %TAGME{ tpaction="" web="SCMP118/Fall2016" tag="" }% view all tags

Shakespeare in Class file activity

Getting ready for the final project - viewing and search the text.

The goal here is to get everyone to step one on the final project. This project involved building a object orient c++ program that allows you to search for words in the complete works of Shakespeare.

What we will do in this hands on activity is to set up a small program that reads in portions of Shakespeare, and displays it on the screen.

Step 1 - files and project setup

  1. Create a C++ project "ssreader" on your system in Netbeans.
  2. Download "Shakespeare.txt" file by right clicking here: Shakespeare.txt and saving it to your system.
  3. Copy the file into the top directory of your project (this will likely be "yourhomedir/NetBeansProjects/ssreader")

Step 2 - Program to read Shakespeare.

  • Consider the following code to create a input file stream, and open it to the Shakespeare file. Add this to your project main function,
#include <iostream>
#include <fstream>
#include <string>

   using namespace std;

    ifstream ssfile;

    // Open the file
    if (!ssfile.is_open()) {
        cout << "File not found:" << filename << " Goodbye!!" << endl;
        return 0;
  • Add a loop to read each line, display it, and ask if the user wants to continue.
char done;
    string s;
    do {
        getline(ssfile, s);
        cout << s << endl;

        cout << "Enter \"c\" to continue, anything else to stop:";
        cin >> done;

    } while (!ssfile.eof() && done == 'c');

Step 3 - Make the program count words

Let's create a function that given an open file stream and a word in a string, runs through a file, counting the number number time that word is in the file.

Consider the following c++string function "find". It's documented here.

Lets start by writing a function "countWords":

int countWords(istream& is, string word);

Search for boat. search for king. It might be nice to print out the lines where you find matches.

Does your program account for the same word more than once in a line? Print out each line as you find it.

Try to make it count EVERY occurrence of the word.

int countWords(istream& is, string word){
    int total = 0;
    string s;
    while (!is.eof()) {
         getline(is, s);
         size_t pos = s.find(word);
         while (pos != string::npos) {
             cout << pos << ":" << s << endl;
             pos = s.find(word,pos+1);
    return total;

Step 4 - Loop the program

Modify code to allow user to loop repeated search for different words.

Problem - file is at the end! What can we do?

  1. First must clear error condition (EOF)
  2. Then must move the "file pointer" back to start.

inputFile.clear(); inputFile.seekg(0, ios::beg);

Step 5 - Search for phrases

What do we need to change to search for a phrase rather than a word?

Step 6 - Additional iteas

  • Print out the names of all the books in the file.
  • Ask for a word, and only display paragraphs with that word.
  • Ask for a word, tell user how many occurrences of it there are in the words.</sticky>
Edit | Attach | Watch | Print version | History: r9 | r7 < r6 < r5 < r4 | Backlinks | Raw View | Raw edit | More topic actions...
Topic revision: r5 - 2017-11-28 - JimSkon
  • Edit
  • Attach
This site is powered by the TWiki collaboration platform Powered by PerlCopyright © 2008-2020 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback