Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Put string into console, with ability edit [closed]

What function in C++ puts string into console, with ability edit? I have such state of console: enter image description here

After work of needed function I want to see this:

enter image description here

but not this:

enter image description here

like image 485
Тарас Avatar asked Oct 30 '22 04:10

Тарас


1 Answers

It cannot be done natively on the terminal, you have to do it in your control flow.

A little example

string text("Hello, World")
cout << text;
char x = getch();
while (x != '\n') {             //loop breaks if you press enter
    if (x == 127 || x == 8) {   //for backspace(127) and delete(8) keys
        cout << "\b \b";        //removes last character on the console
        text.erase(text.size() - 1);
    }
    else {
        cout << x;
        text.append(x);
   }
    x = getch();
}

"\b" is nondestructive backspace. i.e it moves the cursor backwards but doesn't erase. "\b \b" is destructive backspace.

like image 135
jblixr Avatar answered Nov 02 '22 10:11

jblixr