Instruction similar to system("cls") in C

Asked

Viewed 4,365 times

0

The program I am doing works as follows: Whenever an incorrect value is typed, I want the program to warn that the value is incorrect, and soon after clear only the error message so that the user can write another value, but the only instruction I know (system("cls")) clears the screen completely. There is some instruction similar to system("cls") in C, but cleaning only a certain part of the screen, and not the screen totally?

  • 2

    In fact, system("cls") is not guaranteed to clean the screen. What is the call system, of stdlib? It is simply a delegation to the operating system to call an operating system command line. It turns out that the command cls on the Windows terminal causes the terminal to have the write prompt at the top. So it’s not something from C, but an external program you are calling. Just like the system("pause") is calling the program pause windows

  • @Jeffersonquesado I get it. Thanks for the clarification, but do you know any commands to clean only part of the screen? Or some command of C itself that does similar thing?

  • 1

    You would have to use functions from the conio. h library to manipulate the screen more elaborately in MS-DOS/Windows. In UNIX it would be the ncurses library.

  • 2

    Summarizing what Jeff meant, it is not possible to say that something will work, whether in C or outside it, depends on the system and back and forth of settings, but if it is something simple my suggestion would be to copy the "output" (which should be done before sending to the actual output), clear everything, pick up what was copied, manipulate and clean the desired area on its own and then play again, causing the impression that it was cleaned only part but was actually "redesigned". But it’s just a suggestion, it must have some g a m i b a r a lib that does something like this.

  • @epx, not short to conio, and also not many people liked https://answall.com/a/304271/64969

  • @Guilhermenascimento I managed to solve with the manipulation that you indicated, and some more gambiarras, but it worked. But I was able to solve it because the problem was at the beginning of the code, if it was in the middle or at the end it wouldn’t, so your tip helped me in parts. I needed some command that would really clean up just part of the code, without all these tricks of repeating all the code, so it looks like it just wiped out a part, which is I did here.

Show 1 more comment

1 answer

1

Have windows library. h, You edit the pixels using the function:

void gotoxy(int x,int y){// muda a posição do cursor
    COORD p={x,y};
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),p);
}

can use a for command to create or delete:

for(x=0;x<60;x++){//Cria um quadrado
    for(y=0;y<30;y++)
        if(y==0 || x==0 || y==29 || x==59 ){
        gotoxy(x,y);//função para mudar a posição do cursor
        printf("%c",219);//imprime um character no console
        }

To erase is just you put the gotoxy function with x and y in the desired place and add a:

printf("%c",32);//valor 32 = espaço na tabela ascii

Browser other questions tagged

You are not signed in. Login or sign up in order to post.