27

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?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Aayush
  • 3,079
  • 10
  • 49
  • 71
  • 9
    In C++, the header is simply `#include `, **not** `#include ` – GManNickG Sep 04 '09 at 05:49
  • `` may be the standard but `clrscr()` isn't standard, so it actually makes sense to find that in a non-standard header. – MSalters Sep 04 '09 at 08:41
  • 2
    I have actually always wondered and meant to ask why some people always start a program with clrscr() and end with getch() in the first place. – UncleBens Oct 03 '09 at 14:37
  • Related post - [getch is deprecated](https://stackoverflow.com/q/814975/465053) – RBT Jun 15 '18 at 09:19

11 Answers11

30

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.

RBT
  • 24,161
  • 21
  • 159
  • 240
Stephen Veiss
  • 1,364
  • 9
  • 10
9

getch() and clrscr() will work with C++. Include conio.h

However, if you CANNOT (for some reason) include conio.h,

how about cin>>dummy_var with a display message asking the user to press enter?

KJ Saxena
  • 21,452
  • 24
  • 81
  • 109
  • 11
    +1 because most interesting things you can do with C and C++ are actually not in the standard. Neither standard is intended to be complete, and the fact that you can include headers other than the standard-mandated ones is very much by design. – MSalters Sep 04 '09 at 08:42
7

just use cin.get();

anonymous
  • 71
  • 1
  • 1
4

Just use these two functions:

fflush(stdin);
getchar();

Visual studio and Dev C++ include this in its iostream header so no need to include extra header file.

Jon Lin
  • 142,182
  • 29
  • 220
  • 220
Balakrishnan
  • 2,403
  • 2
  • 26
  • 47
  • 6
    http://stackoverflow.com/a/257203/183120 `fflush(stdin)` is undefined behaviour called out in the C standard 7.18.5.2/2. – legends2k Jan 31 '13 at 12:01
3

This is what I usually use:

#include<iostream>
...
std::getchar();
CLearner
  • 532
  • 1
  • 6
  • 17
2

Late answer, you can use std::cin.get(), this should work with most compilers. If that doesn't work, try adding another.

int main () {

    // ...

    std::cin.get();
    std::cin.get();
    return 0x00;
}

Using system("PAUSE") is only available on Windows and is a bad programming habit. The reason for this is it literally pauses or freezes your program as opposed to just waiting for an input. ie. a keypress to exit.

Community
  • 1
  • 1
iKlsR
  • 2,642
  • 6
  • 27
  • 45
1

I understand that this is an old question but I am going to answer nonetheless because people may be looking for an answer to a similar question.

conio.h is an (ancient) Windows and MS-DOS/PC-DOS C library that was, and still is used for very basic, bare-metal keyboard input and handling in a Windows/DOS environment.

Both getch() and clrscr() are non-standard additions by this header, and should be avoided when possible for the standard C functions. getch() can usually be replaced with scanf(), fread(), in C and std::cin and std::cin.get in C++. As for clrscr(), the closest you can get is:

for(int i = 0; i < 100; i++)
{
    printf("\n");
}

OR:

There is also ncurses.h on *nix environments. Here's a link to some info about that.

Mason Watmough
  • 495
  • 6
  • 19
1

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.)

ManuelAtWork
  • 2,198
  • 29
  • 34
1

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)

Aurora087
  • 86
  • 5
-6

You can use system("pause"), which produces the "press any key to continue" message. But it works in the windows environment only. I think all the "system" commands are dos commands. Correct me if I am wrong

  • 1
    You're halfway right. `system()` executes `` using the command processor of whatever OS you're currently on. [Reference](http://www.cplusplus.com/reference/clibrary/cstdlib/system/0). It's also generally considered bad form because it's not portable between platforms. – dwurf Apr 26 '12 at 14:50
-24

if you work on windows you can use system("pause"), this will give you "press any key to continue" message.

bmeric
  • 1,124
  • 1
  • 17
  • 27