Hi everyone!
How can I replace the following functions or OS calls?
system("CLS");
system("PAUSE");
This is because I was told that using OS calls is bad practice. Although it always detected the OS by means of conditionals and, depending on which one, it modified the command.
Many told me to use the function cin
, but by using it, I can type normally. Is there any way to avoid this?:
Note: I don't want to use the conio.h library to solve this problem.
Explanation of the problem
Terminal handling differs between platforms, so it requires using groups
#ifdef
to discriminate the implementation based on the platform your application is compiled on.Block based solution
#ifdef
You can check how to perform the task on different operating systems in the linked article . In it you will find source code of examples related to how to disable keyboard echo in windows and linux, so you can use a block like the one below to achieve your purpose:
Example (obtained from this other stackoverflow answer ):
Usage example:
Solution based on
curses
(ncurses
/pdcurses
)Lastly, you can use
ncurses
orpdcurses
(another cross-platform alternative) to get the job done:In the example I use
getnstr()
to get any key combination until I find a carriage return, likegetch()
. You could use functions that read key by key, but this seemed to me the fastest for the example.Lastly, the work you want is done by the
noecho()
andecho()
functions that disable and enable keystroke echo respectively.Although I am not an expert (or anything similar) in C++, I am going to answer your question with what little I know because at the time I also had similar concerns about using the
system("pause")
and functionssystem("cls")
.To begin with, in relation to the function
system("pause")
you can use the functioncin.get()
, which basically allows you to read a character that you enter from the keyboard (for more exact information you can check the following link ). The way to use it would be similar to the example given by user akko in the other answer.Or you can also use it in the following way, in combination with the function
cin.ignore()
:Now, regarding the function
system("cls)
, unfortunately there is no function in C++ that allows you to "clean" the screen. If I'm not mistaken, I understand that C++ was created to be used in the most general way possible on different platforms, many of which do not even have a screen or something equivalent. In the event that a function that is responsible for cleaning the screen can be implemented, such a function must necessarily depend on the OS and therefore, it would not be portable.As I have read, in case you use a Unix-derived OS you can use the library
ncurses
where there is a function to clean the screen. In the case of Windows you can use the librarypdcurses
, which is analogous to the libraryncurses
(I want to point out that I have not used such libraries so I cannot guarantee anything beyond what I have read). However, as I wrote above, such solutions are OS dependent.An alternative that may work on Windows to clean the screen is to emulate what
system("cls")
. Here is the code (I got it from here ):I've tested it myself and it works quite well.
To substitute
system("pause")
you can do the following.