Can you help me write the following C++ program:  Add a search command to the file viewer created. This command asks the user for a string and finds the first line that contains that string. The search starts at the first line that is currently displayed and stops at the end of the file. If the string is found, then the file will be scrolled down so that when it is redisplayed, the line that contains the string is the first line at the top of the window. If the string is not found, the program prints the following error message at the top of the screen: ERROR: string X was not found where X is replaced by the string entered by the user. In that case, the program redisplays the lines that the user was viewing before the search command was executed. Modify the program as little as possible. Make sure you assign new responsibilities to the most appropriate components. Use the C++ string operation find.   The file viewer code is given below: // FileViewer.cpp #include "FileViewer.h" using namespace std; void FileViewer::display() {     const string long_separator(50, '-');     const string short_separator(8, '-');     system(clear_command);     if (!error_message_.empty()) {         cout << "ERROR: " + error_message_ << endl;         error_message_.clear();     }     string file_name = buffer_.file_name();     if (file_name.empty())         cout << "\n";     else         cout << file_name << endl;     cout << long_separator << endl;     buffer_.display();     cout << long_separator << endl;     cout << "  next  previous  open  quit\n";     cout << short_separator << endl; } void FileViewer::execute_command(char command, bool & done) {     switch (command) {         case 'n': {             buffer_.move_to_next_page();             break;         }         case 'o': {             cout << "file name: ";             string file_name;             getline(cin, file_name);             if (!buffer_.open(file_name))                 error_message_ = "Could not open " + file_name;             break;         }         case 'p': {             buffer_.move_to_previous_page();             break;         }         case 'q': {             done = true;             break;         }     } } void FileViewer::run() {     cout << "Window height? ";     cin >> window_height_;     cin.get();  // '\n'     cout << '\n';     buffer_.set_window_height(window_height_);     bool done = false;     while (!done) {         display();         cout << "command: ";         char command;         cin >> command;         cin.get(); // '\n'         execute_command(command, done);         cout << endl;     } }

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

Can you help me write the following C++ program:

 Add a search command to the file viewer created. This
command asks the user for a string and finds the first line that contains
that string. The search starts at the first line that is currently displayed
and stops at the end of the file. If the string is found, then the file will
be scrolled down so that when it is redisplayed, the line that contains the
string is the first line at the top of the window.
If the string is not found, the program prints the following error message
at the top of the screen:
ERROR: string X was not found
where X is replaced by the string entered by the user. In that case, the
program redisplays the lines that the user was viewing before the search
command was executed.
Modify the program as little as possible. Make sure you assign new responsibilities to the most appropriate components. Use the C++ string operation find.  

The file viewer code is given below:

// FileViewer.cpp

#include "FileViewer.h"

using namespace std;

void FileViewer::display()
{
    const string long_separator(50, '-');
    const string short_separator(8, '-');

    system(clear_command);

    if (!error_message_.empty()) {
        cout << "ERROR: " + error_message_ << endl;
        error_message_.clear();
    }

    string file_name = buffer_.file_name();
    if (file_name.empty())
        cout << "<no file opened>\n";
    else
        cout << file_name << endl;

    cout << long_separator << endl;
    buffer_.display();
    cout << long_separator << endl;
    cout << "  next  previous  open  quit\n";
    cout << short_separator << endl;
}

void FileViewer::execute_command(char command, bool & done)
{
    switch (command) {
        case 'n': {
            buffer_.move_to_next_page();
            break;
        }

        case 'o': {
            cout << "file name: ";
            string file_name;
            getline(cin, file_name);
            if (!buffer_.open(file_name))
                error_message_ = "Could not open " + file_name;
            break;
        }

        case 'p': {
            buffer_.move_to_previous_page();
            break;
        }

        case 'q': {
            done = true;
            break;
        }
    }
}

void FileViewer::run()
{
    cout << "Window height? ";
    cin >> window_height_;
    cin.get();  // '\n'
    cout << '\n';
    buffer_.set_window_height(window_height_);

    bool done = false;
    while (!done) {
        display();

        cout << "command: ";
        char command;
        cin >> command;
        cin.get(); // '\n'

        execute_command(command, done);

        cout << endl;
    }
}

Expert Solution
steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
File Input and Output Operations
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education