title: How can I make the input to get inserted instead of overwritten on the console?
body: <p>I wrote the following header file and included in my code:</p>
  <pre><code>#define ARROW_RIGHT 77
  #define BACKSPACE 8
  #define INSERT 82
  #define F1 59
  #define F2 60
  #define F3 61
  #define F5 63
  #define SPECIAL_KEY(x) (x==0 || x==0xE0)
  
  //Global variable for screen dimensions
  COORD dims;
  
  /*Note: the following method allows you to write a writer a character
  anywhere on the screen. It moves the screen cursor to the same position as well.*/
  void gotoxy(int xpos, int ypos, int col, char ch)
  {
      COORD scrn;
      HANDLE hOuput = GetStdHandle(STD_OUTPUT_HANDLE);
      scrn.X = xpos; scrn.Y = ypos;
      SetConsoleCursorPosition(hOuput, scrn);
      HANDLE  screen = GetStdHandle(STD_OUTPUT_HANDLE);
      SetConsoleTextAttribute(screen, col);
      DWORD useless;
      WriteConsole(screen, &amp;ch, 1, (LPDWORD)&amp;useless, NULL);
      SetConsoleTextAttribute(screen, 15);
      SMALL_RECT windowSize = { 0, 0, 100, 100 };
      SetConsoleWindowInfo(screen, TRUE, &amp;windowSize);
  }
  
  void GetConsoleWindowDims() {
      HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
      CONSOLE_SCREEN_BUFFER_INFO csbiInfo;
      SMALL_RECT srctWindow;
      GetConsoleScreenBufferInfo(hStdout, &amp;csbiInfo);
      srctWindow = csbiInfo.srWindow;
      dims.X = srctWindow.Right;
      dims.Y = srctWindow.Bottom;
  }
  
  COORD GetConsoleCursorPosition(HANDLE hConsoleOutput)
  {
      CONSOLE_SCREEN_BUFFER_INFO cbsi;
      if (GetConsoleScreenBufferInfo(hConsoleOutput, &amp;cbsi))
      {
          return cbsi.dwCursorPosition;
      }
      else
      {
          // The function failed. Call GetLastError() for details.
          COORD invalid = { 0, 0 };
          return invalid;
      }
  }
  
  void moveScreenCursorLeft() {
      HANDLE hOuput = GetStdHandle(STD_OUTPUT_HANDLE);
      COORD pos = GetConsoleCursorPosition(hOuput);
      if (pos.X &gt; 0)
          pos.X--;
      else if (pos.Y &gt; 0) {
          pos.Y--;
          pos.X = dims.X;
      }
      SetConsoleCursorPosition(hOuput, pos);
  }
  
  void moveScreenCursorRight() {
      HANDLE hOuput = GetStdHandle(STD_OUTPUT_HANDLE);
      COORD pos = GetConsoleCursorPosition(hOuput);
      if (pos.X &lt; dims.X)
          pos.X++;
      else if (pos.Y &lt; dims.Y) {
          pos.Y++;
          pos.X = 0;
      }
      SetConsoleCursorPosition(hOuput, pos);
  }
  
  #endif // TEENY_H_INCLUDED
  </code></pre>
  <p>And my code is:</p>
  <pre><code>#include &lt;iostream&gt;
  #include &quot;Teeny.h&quot;
  using namespace std;
  
  //Skeleton of class LinkedText
  class LinkedText{
      struct Node{
          char ch;
          Node * next;
          Node * prev;
      } *head, *tail, *cursorPos;
  
      int textSize;
  
      public:
          LinkedText(){
              head=tail=cursorPos=NULL;
              textSize=0;
          }
          //All these methods (except copying) are O(1) thanks to the linked list!
          void addCharAtCursorPos(char input);//when char entered
          void delCharAtCursorPos();//when bksp pressed
          void moveCursorRight();//right arrow
          void moveCursorLeft();//left arrow
          /*the following method returns the sub-list after cutting,
          and also adjusts cursor position*/
          Node * cutTextBetween(Node * cursorpos1, Node * cursorpos2);
          Node * copyTextBetween(Node * cursorpos1, Node * cursorpos2);
          void insertTextAtCursorPos(Node * subList);
  
         
          int findInText(const string &amp; key);
  
          //You may add appropriate printing methods and other methods below
          ~LinkedText(){//add code
          };//delete the list
  
  
          /*The methods below will be used to keep
            the screen cursor inside the text area
            only.
          */
          inline bool textToLeft(){
              return (cursorPos!=NULL &amp;&amp; cursorPos-&gt;prev!=NULL);
          }
  
  
          inline bool textToRight(){
              return (cursorPos!=NULL);
          }
  
  };
  
  int main()
  {
      GetConsoleWindowDims();
  
      /*This code is capable of detecting the
        left and right arrow keys; the backspace key;
        the escape key (for quit); the F1, F2 and F3 keys;
        and the regular text keys.
        Written for CS-218 &quot;Data Structures&quot; BCS-3A, FAST-NU, Lahore.*/
  
      LinkedText text;
      int key;
      while(key!=ESCAPE){
            key=_getch();
            //either an arrow or function key is pressed
            if(SPECIAL_KEY(key)){
                key=_getch();//get 2nd part of code from buffer
                switch(key){
                case ARROW_LEFT:
                    //if(text.textToLeft()){
                      moveScreenCursorLeft();
                      //text.moveCursorLeft();
                    //}
                  break;
                case ARROW_RIGHT:
                    //if (text.textToRight()){
                      moveScreenCursorRight();
                      //text.moveCursorRight();
                    //}
                  break;
                case F1://start or end text selection
                    cout&lt;&lt;&quot;[F1]&quot;;
                  break;
                case F2://cut option
                    cout&lt;&lt;&quot;[F2]&quot;;
                  break;
                case F3://copy option
                    cout&lt;&lt;&quot;[F3]&quot;;
                  break;
                case F5://for search
                    cout&lt;&lt;&quot;[F5]&quot;;
                  break;
                case INSERT://to insert text
                  cout&lt;&lt;&quot;[INSERT]&quot;;
                  break;
                };
            }else if(key==BACKSPACE){
                cout&lt;&lt;&quot;[BKSP]&quot;;
            }else{
                cout&lt;&lt;char(key);
            }
      }
      return 0;
  }
  </code></pre>
  <p>So this code prints the the specified characters and when I move the cursor through arrow keys, it moves accordingly but when I enter any character, it overwrites the character being pointed by the character. But what I want is to insert the character at the cursor position.</p>