mardi 23 novembre 2021

Erase console line after taking input in C++, weird characters "²²²²▌▌▌▌▌▌5I=D≤@" on cout [duplicate]

Visual C++ in VS 2019

See the below program:

#include <iostream>
#include <conio.h>

using namespace std;

int main() {
 string ask = "Input char: ";
 cout << ask;
 char input = _getch();

 \\ The below part is to erase the previous line from console after getting the input.

 char* spacearray = new char[ask.length()];
 for (int i = 0; i < ask.length(); i++) {
  spacearray[i] = ' ';
 }
 cout << "\r" << spacearray << "\r";
}

The last cout statement does erase the previous line but also puts some weird characters at the end.

For example: ²²²²▌▌▌▌▌▌≤H=â≤@

I can just use a bunch of space inside "" instead of spacearray but that would cause problem if the no of chars in the previous line is more than the no of spaces. And even if I do that, I still want to know where those weird characters come from.

Aucun commentaire:

Enregistrer un commentaire