dimanche 26 juillet 2020

Cursor remains at the end when I try to give user input in getline

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.

Aucun commentaire:

Enregistrer un commentaire