Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alternative function in iostream.h for getch() of conio.h?

Tags:

c++

iostream

I'm trying to hold the screen on my output using the header file <iostream.h>, but I don't know any equivalent function to the getch() & clrscr() functions of <conio.h> in <iostream.h> or any other C++ library. Are there any such functions?

like image 357
Aayush Avatar asked Sep 04 '09 05:09

Aayush


People also ask

What can I use instead of getch in C++?

getchar() will also do the job in C/C++. For a windows specific solution that doesn't wait for the enter key, you can use the system function: system("pause"); system function executes an operating system command.

Can we use Getch without conio?

Technically, as long as the code is not using C++ features, you can use functions without declaring them (but you should enable warnings to avoid this).

Can we use getch in C++?

Here, getch() returns the ASCII value of the character read from stdin . For example, if we give the character '0' as input, it will return the ASCII value of '0', which is 49. Now, in C / C++, we can directly convert a character to an integer.


1 Answers

The conio.h functions are compiler extensions to the language, not part of C or C++. There isn't a direct replacement in standard C++.

For getch(), int ch = std::cin.get(); is probably the closest equivalent -- but bear in mind that this will read from buffered standard input, whereas I think the conio.h getch does an unbuffered read.

Any implementation of clrscr() is going to be very platform-dependent -- not all screens or terminals have a notion of clearing, and those that do have wildly differing ways to access that functionality.

If you need to treat the terminal as something other than a set of character streams, your best bet is probably to look for a library which hides the details of the underlying terminal, screen or console from you. If you're on a UNIXish system, look at the curses or ncurses library; I don't know of any suggestions for other OSes.

like image 65
Stephen Veiss Avatar answered Sep 29 '22 21:09

Stephen Veiss