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; } }
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;
}
}
Step by step
Solved in 2 steps