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

后端 未结 10 1023
无人共我
无人共我 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:34

    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.

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

    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.

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

    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

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

    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.

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