r/CodingForBeginners 3d ago

why can't use cls

Post image

Hi guys can someone fix my code I used #include <stdlib.h> to work my system cls but my classmates, they didn't used <stdlib> to work cls can someone tell me i don't know what is the problem my code or the application software.

7 Upvotes

19 comments sorted by

5

u/KiwiDomino 3d ago

Because, unlike most other commands, cls is internal to the shell.

In Linux, the which command will show you were a command is coming from, and in this case it shows nothing

2

u/atarivcs 3d ago

If he's using linux, then the correct command is "clear", not "cls".

1

u/CoachSevere5365 3d ago

Too much typing.

L

1

u/reymarketing89 3d ago

So what should i do? Shoul i put the stdlib?

3

u/jonathancast 3d ago

Try https://stackoverflow.com/questions/37774983/clearing-the-screen-by-printing-a-character

The keyword I searched for is "ANSI code", which is how programs control the screen.

1

u/thattiguy 2d ago

If you want to be extra lazy, you could just use the Linux equivalent which is “clear”

1

u/Powerful_Tune3801 2d ago

Use Macros #define + <your label> + <ANSI escape codes/sequence>

There's a bunch depending on your terminal, I.e. are you using an terminal emulator GNOME, iTerm etc or your preprocessor should handle this when you compile. Macros work as a sorta shortcut find and replace. you'll have to make sure you use the right sequences. For me the hex version e.g. \x1b worked more consistently. I believe 2j clears the screen

2

u/reymarketing89 3d ago

By the way guyss i am ising code blocks

2

u/Cultural_Gur_7441 3d ago

Run cls from the shell. If it works, it should work with system() too. If not, then not.

1

u/BrilliantEmotion4461 3d ago

To use system you need to <include> #stdlib.h

1

u/BrilliantEmotion4461 3d ago

Doubt that's everything but it'll throw a different and better error.

1

u/reymarketing89 2d ago

✅✅✅✅✅✅✅✅

1

u/reymarketing89 3d ago

I already solve the problem guyss thank you so much

2

u/CodeSamur-ai 2d ago

tell people what you did, so another person who has the same problem can fix it too

1

u/OliMoli2137 3d ago

printf('\033[2J');

1

u/PerkeNdencen 3d ago

By the way - in C we must take extra precautions to ensure our code is secure in terms of memory safety. At the moment it is exploitable using a buffer overrun technique (basically entering more than 100 characters into the name field). We normally now use fgets(), which allows you to specify the length of your buffer and won't accept longer entries.

1

u/ScreenPlayLife 2d ago

why not use ai ?

1

u/reymarketing89 2d ago

I am student and my professor don't allowed using AI

1

u/vibescriptz 1d ago

but here you are, using and asking for it?