1

Code:

#include <iostream>
#include <string>
using namespace std;

int main(){
    string s;
    cout << "Enter a string : " << endl;
    cin >> s;
    cout << "The Entered String is : " << s << endl;
    cout << "The Length of Entered String is : " << s.length() << endl;
    return 0;
}

Output:

┌─[jaysmito@parrot]─[~/Desktop]
└──╼ $g++ -o trycpp -Os try.cpp
┌─[jaysmito@parrot]─[~/Desktop]
└──╼ $./trycpp
Enter a string : 
hello
The Entered String is : hello
The Length of Entered String is : 5
┌─[jaysmito@parrot]─[~/Desktop]
└──╼ $./trycpp
Enter a string : 
hello^[[C
The Entered String is : hello
The Length of Entered String is : 8

Same thing happens with C also if you want the code in C please ask for it!

When I press the arrow keys ^[[C show up instead of the cursor moving (similar thing happens with other arrow keys, escape key, home, end)!

Whats happening here is the second time the string has the characters :

['h', 'e', 'l', 'l', 'o', '\x1b', '[', 'C']

So, the '\x1b', '[', 'C' is the sequence of characters send to the shell from the keyboard for representing right arrow key(cursor forward).

What i want is that these characters will not show up in the shell but the cursor will move (forward, backward, to home, end, etc as per the key pressed).

Processing after taking the input is meaning less as the main aim is to let the cursor be moved!

How can i achieve this in C or C++?

[EDIT]

The one and only aim is to give the user a exact terminal like experience while using the program.

Jaysmito Mukherjee
  • 1,467
  • 2
  • 10
  • 29

1 Answers1

2

Cursor movement, function keys and other special keys are handled differently in different terminals. But in most cases some escape sequence is generated.

Program prompt in most languages is a simple read from stdin, it will read escape sequences as-is. So e.g. ←[D, ←[C correspond to left/right cursor movement.

If you want to handle these escape sequences like bash prompt does, then you need to do that yourself or use a 3rd-party library.

A popular one is GNU readline, but there are alternatives, e.g. libedit.

Here's a demo program with libedit (need to install libedit first: sudo apt install libedit-dev):

#include <editline/readline.h>
#include <stdio.h>

int main() {
    char *line;
    while (line = readline("Enter input: ")) {
        printf("Entered: %s\n", line);
    }
    return 0;
}

Compile and run:

$ g++ test.cpp -o test -ledit
$ ./test

Now you should be able to move around the input line with left/right keys.

rustyx
  • 80,671
  • 25
  • 200
  • 267
  • Thanks it works perfectly!!!! (a simple question is it possible without any library) – Jaysmito Mukherjee Mar 31 '21 at 11:25
  • Of course it's possible if a library does it. It's just not very easy. [Here's](https://github.com/cdesjardins/libedit/blob/master/src/parse.c#L139) how to parse an escape sequence and [here's](https://github.com/cdesjardins/libedit/blob/master/src/terminal.c#L549) how to send control sequences to the terminal to move the cursor position. – rustyx Mar 31 '21 at 11:29
  • Thanks a lot for the resource – Jaysmito Mukherjee Mar 31 '21 at 11:30
  • i used the way it worked and made a Java Library for it :- https://stackoverflow.com/a/66901865/14911094 also compiled a list of solutions for all the languages :- https://stackoverflow.com/a/66902152/14911094 – Jaysmito Mukherjee Apr 01 '21 at 10:41