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

Viewed 102336

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?

11 Answers

The platform-specific function getch() from conio.h has two special features:

  • No echoing of characters.
  • Unbuffered reading of characters.

The echoing is done by the terminal outside of the C/C++ environment. It can only be controlled by manipulating the terminal. Also, it is nearly impossible to get unbuffered I/O with the iostream.h header.

Therefore it is not possible to get anywhere near getch() using iostream.h alone.

(There are plenty of getch()implementations around, e.g. using termios.h to disable echoing.)

For clrscr() I generally use

system("cls");       //Windows
system("clear");     // Linux

And for the getch() function I agree with @iKlsR , just use cin.get() 2 times.

{   ...
    std::cin.get();
    std::cin.get();
}

and if you are looking to pause the system for a moment or specific time interval use:

system("sleep 5s"); //for linux

basically the function system("x") runs the command x in terminal. So you can just exploit it by using the different function in place of x. (be specific to use the commands which are related to your distro or OS)

Related