The following is the the code for asking a name and then returning a statement which has your name in it.
#include <iostream>
#include <string>
int main(){
std::string name;
std::cout << "What is your name:\n";
std::getline(std::cin, name);
std::cout << "Your name is " << name << ".\n";
return 0;
}
When I run this program and type the name, sometimes you type the input in wrong manner and hence you use the arrow keys to go left or right. However, when I use the left arrow key, instead of the cursor going to the left, ^[[D
is printed. And when I use the right arrow key, ^[[C
is printed.
My question is how can I avoid this and actually move the cursor to the right or left. I use Vim text editor on Linux.
The usual way to handle this sort of thing is the use the editline or GNU readline linrary (which is a C libary):
#include <stdio.h>
#include <editline/readline.h>
#include <editline/history.h>
int main() {
const char *name = readline("What is your name:");
printf("Your name is %s.\n", name);
return 0;
}
You need the libray installed, and you link your program with -leditline,
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.