In C++ Write a simple word processor that will accept text and commands as input
ID: 3859915 • Letter: I
Question
In C++
Write a simple word processor that will accept text and commands as input. The commands it should respond to are:
Substitute/oldstring/newstring/
Copy #
Locate/string/
Delete #
Move #
Type #
Pastes
Insert #
Replace #
Quit
The file will consist of up to 100 lines. The program should be able to open an existing file or create a new one. It should keep track of where the user is in the file at any time. Each command is followed by a number which indicates the number of line (the current line & those following) upon which the command should act. Each command should operate on the specified line or lines of the file. A detailed explanation of each command follows:
Substitute/oldstring/newstring
Substitute newstring for every occurrence of oldstring in the current line. Issue a message if oldstring does not appear in the current line. Print the changed line.
Type #
Print the contents of the next # lines, including the current one. Don’t print blank spaces at the end of a line. The current line should be the last typed.
Copy #
Copy the next # lines, including the current one, to a temporary storage area. The current line should not change.
Paste
Copy the contents of the temporary storage area to a position between the current line and the line following the current line. The current line should become the last line pasted.
Locate/string/
Find the next occurrence of the string and make the line containing it the current line. If string does not occur anywhere in the file after the current line, issue a message and don’t change the current line. Then print the current line.
Insert #
Insert # lines into the file following the current line. The current line should be the last line entered.
Delete #
Delete the next # lines, including the current one. Make the first line following the deleted section the current line.
Replace #
Replace the next # lines, beginning with the current one, with # lines. The current line should be the last line entered.
Move #
Locate & print the line # lines past the current line. Make that the current line.
Quit
Saves the file to disk and quits the editor
Attach A picture showing that the code compiled and running please
Substitute/oldstring/newstring/
Copy #
Locate/string/
Delete #
Move #
Type #
Pastes
Insert #
Replace #
Quit
Explanation / Answer
Solution)
-Word processing allows you to treat text like modeling clay. You can easily mold your words and ideas. You can change margins, put in underlines, italics, boldface, and so forth.
-You can correct errors, move text around, lay it out on the page all at the touch of a few buttons. In the next lesson you'll have an opportunity to become quite sophisticated in this regard. But let's keep it simple for now.
-Use the I–beam cursor to position the insertion point cursor right after the zip code in the first address at the top of the page, then press Enter
-The cursor should now be waiting at the beginning of the next line. From the Insert menu select Date and Time.
Type #
Print the contents of the next # lines, including the current one. Don’t print blank spaces at the end of a line. The current line should be the last typed.
Adding text#
This is an activity which is fundamental to the whole process of writing. On a typewriter it is usually messy, and often difficult to do. When you use the word processor, however, it is a simple task.
#include <iostream>
#include <fstream>
using namespace std;
int main() {
char filename[81];
char input_line[81]; // Input line for text entry
cout << "Enter a file name and press ENTER: ";
cin.getline(filename, 80);
ofstream file_out(filename);
if (! file_out) {
cout << "File " << filename << " could not be opened."; //if file opening fails
return -1;
}
cout << "File " << filename << " was opened." << endl;
while (1) {
cout << "Enter line (~~~ to quit)>>";
cin.getline(input_line, 80);
if (strcmp(input_line, "~~~") == 0)
break;
file_out << input_line << endl;
}
file_out.close();
return 0;
}
MAKING A BACKUP COPY OF THE DOCUMENT
Your last task before completing this session at the computer is to make a backup of your document on another disk. Earlier in this lesson you were asked to prepare a backup disk for all your work. Now is the time to learn how to make a backup copy of a single file. The Conference Call letter (ConfCall) is still open in Word. It is also saved on your Data disk, which is in the disk drive.
-Close the Word word processing program
-Close or minimize any other windows that may be open on your desktop to make it easier for you to see what you’re doing Double click to open the My Computer icon, then double click on 3 1/2 floppy A:
-Now drag the folder WPFiles from your Data disk to the Desktop and drop it there
-Watch while Windows makes a copy of your files on to the desktop, then close the Window on your Data disk
Remove your Data disk from the disk drive, replace it with your DataBkp disk and double click on 3 1/2 floppy A: -Now drag the WPFiles folder from the desktop to your DataBkp disk