C++ iostream.h 中 conio.h 的 getch() 的替代函数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1377403/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Alternative function in iostream.h for getch() of conio.h?
提问by Aayush
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?
我正在尝试使用头文件将屏幕保持在我的输出上<iostream.h>
,但我不知道任何getch()
与in或任何其他 C++ 库的&clrscr()
函数等效的函数。有没有这样的功能?<conio.h>
<iostream.h>
采纳答案by bmeric
if you work on windows you can use system("pause"), this will give you "press any key to continue" message.
如果您在 Windows 上工作,您可以使用系统(“暂停”),这会给您“按任意键继续”消息。
回答by 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++.
这些conio.h
函数是语言的编译器扩展,而不是 C 或 C++ 的一部分。标准 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.
对于 getch(),int ch = std::cin.get();
可能是最接近的等价物——但请记住,这将从缓冲的标准输入中读取,而我认为 conio.hgetch
会进行无缓冲读取。
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.
的任何实现clrscr()
都将非常依赖于平台——并非所有屏幕或终端都有清除的概念,而那些确实有非常不同的方式来访问该功能。
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.
如果您需要将终端视为一组字符流以外的东西,最好的办法可能是寻找一个库,它对您隐藏底层终端、屏幕或控制台的详细信息。如果您使用的是 UNIX 系统,请查看 curses 或 ncurses 库;我不知道对其他操作系统有什么建议。
回答by Kshitij Saxena -KJ-
getch() and clrscr() will work with C++. Include conio.h
getch() 和 clrscr() 将适用于 C++。包括 conio.h
However, if you CANNOT (for some reason) include conio.h,
但是,如果您不能(出于某种原因)包含 conio.h,
how about cin>>dummy_var with a display message asking the user to press enter?
cin>>dummy_var 显示消息要求用户按 Enter 怎么样?
回答by anonymous
just use cin.get();
只需使用 cin.get();
回答by 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.
Visual Studio 和 Dev C++ 将其包含在其iostream
头文件中,因此无需包含额外的头文件。
回答by CLearner
This is what I usually use:
这是我通常使用的:
#include<iostream>
...
std::getchar();
回答by iKlsR
Late answer, you can use std::cin.get()
, this should work with most compilers. If that doesn't work, try adding another.
迟到的答案,你可以使用std::cin.get()
,这应该适用于大多数编译器。如果这不起作用,请尝试添加另一个。
int main () {
// ...
std::cin.get();
std::cin.get();
return 0x00;
}
Using system("PAUSE")
is only available on Windows and is a badprogramming habit. The reason for this is it literally pausesor freezesyour program as opposed to just waiting for an input. ie. a keypress to exit.
使用system("PAUSE")
仅在 Windows 上可用,是一种不良的编程习惯。这样做的原因是它实际上会暂停或冻结您的程序,而不仅仅是等待输入。IE。一个按键退出。
回答by Mason Watmough
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.
conio.h
是一个(古老的)Windows 和 MS-DOS/PC-DOS C 库,过去和现在仍然用于 Windows/DOS 环境中非常基本的裸机键盘输入和处理。
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:
两个getch()
和clrscr()
非标准增加由这个标题,并且应该避免当可能的是,标准C函数。getch()
通常可以用替换scanf()
,fread()
在C和std::cin
和std::cin.get
在C ++中。至于clrscr()
,你能得到的最接近的是:
for(int i = 0; i < 100; i++)
{
printf("\n");
}
OR:
或者:
There is also ncurses.h
on *nix environments. Here's a linkto some info about that.
回答by ManuelAtWork
The platform-specific function getch()
from conio.h has two special features:
getch()
conio.h 中特定于平台的函数有两个特殊功能:
- No echoingof characters.
- Unbufferedreading 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.
回显由 C/C++ 环境之外的终端完成。它只能通过操作终端来控制。此外,几乎不可能使用 iostream.h 标头获得无缓冲的 I/O。
Therefore it is not possible to get anywhere near getch()
using iostream.h alone.
因此,getch()
单独使用 iostream.h是不可能的。
(There are plenty of getch()
implementations around, e.g. using termios.h to disable echoing.)
(getch()
周围有很多实现,例如使用 termios.h 禁用回显。)
回答by rockstar
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
您可以使用 system("pause"),它会产生“按任意键继续”消息。但它仅适用于 windows 环境。我认为所有的“系统”命令都是 dos 命令。如果我错了,请纠正我