I am asked to create a program that will enable the user to input something just like a text editor.
Saving of the file is not required. We just have to let the user type in it(on the console/dos environment of the turboc++) just like any other text editor.

What I have done so far is letting the user go through the position of each letter, creating a new line when enter key is pressed and erasing the previous character when backspace is pressed.

here is my code.

#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define LEFT 2
#define TOP 3
#define BOT 22
#define RIGHT 77

#define WIDTH (RIGHT-LEFT+4)
#define HEIGHT (BOT-TOP+7)

#define L_ARROW 75
#define R_ARROW 77
#define U_ARROW 72
#define D_ARROW 80
#define ENTER 13
#define ESC 27
#define BP 8

int buff [WIDTH][HEIGHT];

void Project1(){

int x,y;
char key, keyb;
x=1;y=1;
gotoxy(1,1);

    while(key != ESC)
    {
        if(kbhit){
            key =  getch();
            if(key == 0)
            {
                switch(getch())
                {
                    case L_ARROW:
                    if(x>1)
                    gotoxy(--x,y);
                    break;

                    case R_ARROW:
                    if(x<WIDTH)
                    gotoxy(++x,y);
                    break;

                    case U_ARROW:
                    if(y>1)
                    gotoxy(x,--y);
                    break;

                    case D_ARROW:
                    if(y<HEIGHT)
                    gotoxy(x,++y);
                    break;
                }
            }

            else if(key == ENTER){
                printf("\n");
            }

            else if(key==BP)
            {
                x--;
                gotoxy(x,y);
                printf(" ");
                gotoxy(x,y);
                gettext();
            }

            else{
                putch(key);
                x=wherex();
                y=wherey();
            }
        }
    }
    getch();
}

int main(){
clrscr();
Project1();
}

here is my problem:
If I insert a char on between of other chars, the char on the right side should proceed on the next position as well as the others. I do not want the char(on which the cursor is on) to be just replaced when I insert on it. Aand when I want a new line in between of chars, a new line should be printed and the right side chars should proceed on the new line created.

Any suggestions? they said linked lists is a solution. But I find it hard to apply.
Regards.

Member Avatar for iamthwee

A text editor in a console program is non-trivial.

Member Avatar for iamthwee

.

If you are writing a text editor, you will need to store the characters being typed in into a buffer, and move characters right one place when you want to insert a new character. It would then be just a matter of redisplaying the buffers contents on the screen, with the new character in place.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.