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

后端 未结 10 1022
无人共我
无人共我 2020-11-30 09:55

I\'m trying to hold the screen on my output using the header file , but I don\'t know any equivalent function to the getch() &

相关标签:
10条回答
  • 2020-11-30 10:16

    This is what I usually use:

    #include<iostream>
    ...
    std::getchar();
    
    0 讨论(0)
  • 2020-11-30 10:19

    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?

    0 讨论(0)
  • 2020-11-30 10:20

    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.

    0 讨论(0)
  • 2020-11-30 10:21

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

    0 讨论(0)
  • 2020-11-30 10:25

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

    0 讨论(0)
  • 2020-11-30 10:33

    just use cin.get();

    0 讨论(0)
提交回复
热议问题