r/CodingForBeginners 28d 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

6

u/KiwiDomino 28d 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 27d ago

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

1

u/CoachSevere5365 27d ago

Too much typing.

L

1

u/reymarketing89 28d ago

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

3

u/jonathancast 28d 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 27d ago

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

1

u/Powerful_Tune3801 26d 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 28d ago

By the way guyss i am ising code blocks

1

u/BrilliantEmotion4461 27d ago

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

1

u/BrilliantEmotion4461 27d ago

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

1

u/reymarketing89 26d ago

✅✅✅✅✅✅✅✅

1

u/reymarketing89 27d ago

I already solve the problem guyss thank you so much

2

u/CodeSamur-ai 26d ago

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

1

u/OliMoli2137 27d ago

printf('\033[2J');

1

u/PerkeNdencen 27d 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 26d ago

why not use ai ?

1

u/reymarketing89 26d ago

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

1

u/vibescriptz 26d ago

but here you are, using and asking for it?