function cin.get() and getch()

hi, everyone..can i know what is the difference between cin.get() and getch()?
as what i know, this two functions can hold on the window when giving output, is that any other use of cin.get and getch?
getch()? Is that from conio.h?

get will extract one character from the stream buffer.

This will print the contents of a file:
std::ifstream input("text.txt");
int ch;
while((ch = input.get()) != -1)
  std::cout << (char)ch;

I would recommend that at some point you run your programs from the console, as this will avoid that whole "console closing down" issue.
getch is non-standard and won't work in all compilers, so you shouldn't use it.
cin.get() is standard, but it's still the wrong way to do what you want to do.

The correct way to hold the console open until the user presses enter:
#include <iostream>
#include <limits>

int main()
    std::cout << "Press ENTER to continue..." << std::flush;
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
The other methods you mentioned require you to type a character and then press enter. This method only requires you to press enter.
^^ cin.get() works with only hitting enter .
My compiler must be non-standard then, because if I hit enter it keeps waiting and waiting, and I have to type a character and then press enter. (It's a Microsoft compiler, bu still, it shows that cin.get() is not reliable across compilers)
thanks all..LowestOne, is it mean that if want use getch(), it should used with conio.h?
conio.h is deprecated and non-standard. That means you shouldn't use it because it's not supported by most compilers.
Last edited on
Topic archived. No new replies allowed.