C ++ Clean only part of the screen

2

This is my code:

#include <iostream>
using namespace std;
int main ()
{
int edad;
string nombre;
cout<<"Hola mundo"<<endl;
cout<<"Como estan?"<<endl;
cout<<"Ingresa tu edad"<<endl;
cin>>edad;
cout<<"Ingresa tu nombre"<<endl;
cin>>nombre;
//Digamos que la persona ingreso numeros en el nombre
//Quiero borrar la ultima parte y no todo
return 0;
}

What I want to do is delete a line from the console, the above is just an example.

In a nutshell I do not want to make system ("cls") just delete a part of the screen.

How do I do it?

    
asked by Malthael 14.09.2016 в 22:06
source

2 answers

2

I solved this problem using a combination of the gotoxy function and system ("clear") here I leave the code of the gotoxy function x if someone else needs it:

void gotoxy(int x,int y){
      HANDLE hcon;
      hcon = GetStdHandle(STD_OUTPUT_HANDLE);
      COORD dwPos;
      dwPos.X = x;
      dwPos.Y= y;
      SetConsoleCursorPosition(hcon,dwPos);  }

Do not forget to add the libraries to windows.h and stdlib.h , since they can create functions with loops to clean parts of the screen

    
answered by 06.12.2016 / 22:58
source
4

You can use cout<<"\e[A"; but in your case when printing the last line you use endl , you should use this instruction twice:

int main ()
{
cout<<"Hola mundo"<<endl;
cout<<"Como estan?"<<endl;
cout<<"Esta linea dice x cosa"<<endl;
//quiero borrar solo la linea de arriba y no todo
cout<<"\e[A";
cout<<"\e[A";
return 0;
}

You would only have output:

Hola mundo
Como estan?

What you do when you output "\e[A" is a history-search backward .

    
answered by 15.09.2016 в 01:24