Text Line Editor Program
- Very Simple Version

This is a very simple text line editor, it does not even save anything to
a file or allow the deletion of lines.   Nevertheless, the simple command
dispatcher loop structure introduced here means that new functionality,
such as saving data to files, deleting lines, display of the file directory
and string search functions can be added as modules in the command
dispatcher.

Once we introduce functions, we will be able to simplify and shorten
the program while adding functionality, demonstrating the power and
utility of functions.

As we progress emphasis will be on dealing with the human interface;
while this program as it exists here will go out of control when a letter is
entered where a number is expected, these problems will be dealt with
one at a time.

In order to deal with multiple lines of text data, a two dimensional
character array is introduced even though this is not formally covered
until toward the end of the class, enough detail will be covered in class
to deal with this level of complexity.


You can just cut this program and paste it into Visual Studio:

// File Editor Demonstration Program - Simple // Jim Engel // Version April 5, 2011 /* This is a class project intended to demonstrate a primitive line by line text editor, including command dispatcher operation. This editor is of course not a replacement for the many text editors in use, primarily because it stores text line by line with no provision for fold over. In principle, it could maintain a list style of text file. ***************************************************/ #include <iostream> #include <cstring> using namespace std; const int LINE_LEN = 81; // length of each text line const int LINE_CNT = 14; // max number of lines in edited text const int CMD_SZ = 40; // size of the command string. // MAIN PROGRAM int main( ) { int mark; // General purpose counter int line; // Line marker int count; // used as line counter char command[CMD_SZ]; // User command text srring char text[LINE_CNT][LINE_LEN]; // Text lines being manipulated for ( mark = 0; mark < LINE_CNT; ++mark ) { // Initialize our main text storage text[mark][0] = 0; } cout << "\nTHE VERY SIMPLE HANDY DANDY TEXT LINE EDITOR.....\n"; /***************** COMMAND DISPATCH LOOP ****************/ while ( true ) { cout << "\nEnter a command: "; cin.getline(command, CMD_SZ); command[3] = 0; // Limit string to 3 char. cout << "Command: "<< command << "\n"; if ( !strcmp(command, "end")) /*** END Command ***/ { break; } else if( !strcmp(command, "hel")) /*** HELP Command ***/ { cout << "\nCommands are: \n" "HELP Display commands\n" "END Exit from this program\n" "CHANGE a line of text\n" "LIST current text lines\n" "\n"; } else if( !strcmp(command, "lis")) /*** LIST Command ***/ { for ( count = 0,mark = 0; mark < LINE_CNT; ++mark) { // Find out how many lines, if ( text[mark][0] != 0 ) // including empty lines. count = mark; } for ( line = 0; line <= count; ++line ) { cout << line << ": " << text[line] << endl; } cout << "Number of lines: " << count << endl; } else if( !strcmp(command, "cha") ) /*** CHANGE Command ***/ { cout << "Line to chanage:"; cin >> line; if ( line < 0 || line >= LINE_CNT ) { cout << "Invalid line " << line << endl<< endl; continue; } cout << "New line text: " ; cin.ignore ( ); cin.getline ( text[line], LINE_LEN ); } else cout << "Your command is NOT valid!\n"; } cout << "\nDats all folks.....\n\n"; return 0; }
Text Ed Screen Shot