问题
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?
回答1:
if you work on windows you can use system("pause"), this will give you "press any key to continue" message.
回答2:
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.
回答3:
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?
回答4:
just use cin.get();
回答5:
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.
回答6:
This is what I usually use:
#include<iostream>
...
std::getchar();
回答7:
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.
回答8:
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.
回答9:
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.)
回答10:
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
来源:https://stackoverflow.com/questions/1377403/alternative-function-in-iostream-h-for-getch-of-conio-h