#how to clear cmd screen

103 messages · Page 1 of 1 (latest)

glacial bay
#

hi,

can anyone tell me how to clear the cmd screen, I have commeneted where it should be "//to clear the screen from here"


#include <stdio.h>
#include <stdlib.h>

int main()
{

    int hp = 100;
    int choice;


    while (hp > 0){

    //to clear the screen from here



    printf("1.Attack\n\n");
    printf("2.Magic hit\n\n");
    printf("3.ultimate\n\n");

    printf("enemy hp %d:\n\n", hp);

    scanf ("%d", &choice);



        if(choice == 1){

            hp = hp - 20;
            printf("\nhit for 20\n");

        }
        if(choice == 2){
            hp = hp - 25;
            printf("\nmagic hit for 25\n\n");

        }
        if(choice  == 3){
            hp = hp - 50;
            printf("\nultimate for 50\n\n");
        }
        //printf("\n\nenemy hp: %d\n\n", hp);
    }
        printf("\nenemy defeated\n");



    return 0;
}


azure hazelBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

clever cobalt
#

You can use ANSI escape codes.

glacial bay
#

what is that?

clever cobalt
#

You can try searching.

glacial bay
#

ESC[2J erase entire screen

crimson beacon
#

would: ```c
#include <stdlib.h>

int main(void) {
system("clear");
return 0;
}```
work?

but I think it's a buffer overflow hazzard to use system() like that lol

clever cobalt
#

There's no such hazard here, just that this is not portable.

crimson beacon
#

yeah I guess

glacial bay
#

"'clear' is not recognized as an internal or external command,
operable program or batch file."

while
system("clear");

crimson beacon
#

you are on windows

#

window's command is cls

#

but I'd recommend @clever cobalt solution

#

printf("ESC[2J");

clever cobalt
#

That's not how it's written.

crimson beacon
#

o oh

#

nvm

clever cobalt
#

"ESC" there is just placeholder of the actual character.

#

It is an actual character called "escape".

crimson beacon
#

yes I got it

#

so

#

printf("%c[2J", 27);

#

??

#

seems good enough

glacial bay
#

nah, that just prints "←[2J"

crimson beacon
#

🗿

#

@glacial bay

#
#include <stdio.h>

int main(void) {
    printf("Hello world\n");
    
    printf("\033[H\033[J");

    return 0;
}```
glacial bay
#

that prints
←[H←[J

crimson beacon
#

which terminal are you using

glacial bay
#

terminal?

crimson beacon
#

this

#

or this

clever cobalt
crimson beacon
glacial bay
#

oh

clever cobalt
#

Windows terminal enables ANSI escapes by default, the legacy console host requires an explicit toggle.

clever cobalt
glacial bay
#

im using C, on a compiler called Code Blocks

clever cobalt
#

There is a way to write the character directly into the string literal.

glacial bay
#

found it

#

system("cls");

crimson beacon
glacial bay
#

it works

crimson beacon
#

bruh

clever cobalt
crimson beacon
clever cobalt
glacial bay
#

i just ran it

#

it clears

clever cobalt
#

You understand what "portable" means?

crimson beacon
clever cobalt
#

CodeBlocks has nothing to do with ANSI escapes.

crimson beacon
#

It has a built in terminal

clever cobalt
#

The program is run in a terminal, and thus it's up to the terminal.

clever cobalt
clever cobalt
#

CodeBlocks runs its program in the Windows console host.

crimson beacon
#

Alright

clever cobalt
#

And then:

the legacy console host requires an explicit toggle.

tacit trellis
#

Just use include stdlib and use system("cls")

#

You are only compiling for windows right?

glacial bay
#

i am windows, yes

clever cobalt
#

Here's demonstration code:

#include <stdio.h>

int main(void) {
    printf("This doesn't work.\n\x1B[2JIf you don't see anything above this line, it's working.\n");
}
glacial bay
#

i found it on google

#

system("cls");

clever cobalt
#

That is not portable, meaning, it may not work in other environments.

tacit trellis
crimson beacon
#

@glacial bay The system("cls") works only in windows

tacit trellis
crimson beacon
#

if another user tries to run it on linux it wouldn't work

clever cobalt
#

Even in the same Windows.

tacit trellis
#

how?

clever cobalt
#

A different shell.

tacit trellis
#

*true

crimson beacon
clever cobalt
#

This can be used on Windows to enable ANSI escapes:

const HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);
DWORD consoleMode;
GetConsoleMode(console, &consoleMode);
SetConsoleMode(console, consoleMode | ENABLE_VIRTUAL_TERMINAL_PROCESSING);
crimson beacon
clever cobalt
#

Macro switches that only include that code when compiling for Windows.

tacit trellis
#
#include <stdio.h>
int main(){
    printf("\033[2J\033[1;1H");
    return 0;
}
#

Just use this op

#

Doesn't this work?

crimson beacon
#

it doesn't

tacit trellis
#

How?

crimson beacon
#

because he has ansi off (off by default on windows console host)

tacit trellis
#

What terminal is OP using?

clever cobalt
#

Does on Windows terminal and doesn't on the console host which CodeBlocks launches.

#

It's a sour state, everyone admits, but it is how it is.

tacit trellis
clever cobalt
#

Yes, that's the console host.

#

Meanwhile here's my Minesweeper on the Windows terminal:

tacit trellis
#

Meanwhile, I can't even get a sudoku puzzle to solve

#

We are going off topic though

glacial bay
#

im gonna make this solved

#

if you guys are finished

tacit trellis
#

system("cls") worked right?

#

It's up to the programmer I guess

clever cobalt
#

Works for now.

glacial bay
#

system("cls");

worked, yes

#

!solved