I'm making an options menu using do while
quite simple, but after watching some videos on yt I decided to try the so-called "options menu with arrows", where there is an arrow of the shape ==>
that points to each option in the menu and that scrolls using the arrow keys on the keyboard.
In the videos I saw the library conio.h
or a variant is used conio2.h
. The authors of the videos use this library to be able to call the function getch()
that, as I understood, is used to use the directional arrows.
In one of the videos, for example, the following is done:
#define TECLA_ARRIBA 72
#define TECLA_ABAJO 80
#define ENTER 13
// acá va el código del menú
do
{
tecla = getch();
}
while(tecla != TECLA_ARRIBA && tecla != TECLA_ABAJO && tecla != ENTER);
My question is that I don't know if it's correct to use libraries like conio.h
or conio2.h
because I read on certain websites that these libraries are deprecated, in addition to the fact that their use is a bad practice in C++.
And so I was wondering how you can use directional arrows in C++ without using such libraries. On the other hand, I have tried one of the programs that I saw in VS 2017 and if it compiles, as long as it changes getch()
to _getch()
. Does this imply that there is no problem using the library conio.h
in VS, or should using such a library be avoided anyway?
Thank you in advance for your comments and/or answers.
conio.h
it is not standard.The problem is not so much that they are deprecated as that they are not standard.
conio.h
it is not part of the C standard libraries . If it is a standard C library, the correct way to use it in C++ would be to use the C++-adapted libraries that have ac
prefix and lack an extension. So, if itconio.h
were a standard library, its version in the C++ standard would becconio
. See this thread for more details.What is it
conio.h
?This header is a utility provided by the platform compiler, it declares several functions to allow the user to interact with the console and the functions provided vary between compilers and platforms, in short it does not follow neither ANSI C nor POSIX . That is why its use is not recommended, as it makes the code unportable .
Alternatives.
Unfortunately, C++ does not have any standard utility to deal with that kind of user interaction. The console is read through as
std::cin
it is a layer on top of the data buffers that returns already processed information, the arrow keys are not considered data.To avoid the use of
conio.h
you should consult the documentation of your platform yourself and find out how the arrow keys can be consulted, which brings us back to the first point: since you are not using standard headers, your code will not be portable and consequently will not be its use is recommended.Instead of using platform dependent headers, you could make use of third party libraries that abstract this process, I understand that Boost.Asio is capable of this, but I can't say for sure as I haven't tested it.
Is it so bad not to be portable?
It is not strange that it is so. If portability is not a requirement of your program, using platform-dependent headers should not be a problem. However, not using standard headers can sometimes be problematic apart from the portability problem: these headers can change more frequently than the standard ones and thus be incompatible or behave differently from version to version.
Symbols beginning with an underscore (
_
) are not reserved words but are reserved for internal compiler use, you should not use them in your code.