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

坚强是说给别人听的谎言 提交于 2019-11-27 14:59:11

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

Stephen Veiss

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.

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?

just use cin.get();

bkmagnetron

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.

This is what I usually use:

#include<iostream>
...
std::getchar();
iKlsR

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.

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.

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

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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!