What is the code to press enter?

I'm writing a program and I want the input to automatically press enter after the keystroke. What is the code to do this? Thanks in advance
You are asking for unbuffered input.

Are you on Windows?
Or Unix/Linux?
Or something else?

On Windows, just use SetConsoleMode()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <windows.h>

int main()
  {
  HANDLE hstdin;
  DWORD  mode;

  hstdin = GetStdHandle( STD_INPUT_HANDLE );
  GetConsoleMode( hstdin, &mode );
  SetConsoleMode( hstdin, ENABLE_ECHO_INPUT | ENABLE_PROCESSED_INPUT );  // see note below 

  cout << "Press any key..." << flush;
  int ch = cin.get();

  if (ch == 13) cout << "\nYou pressed ENTER\n";
  else          cout << "\n\nYou did not press ENTER\n";

  SetConsoleMode( hstdin, mode );
  return 0;
  }


Note: See the MSDN documentation for more on the console mode:
http://www.google.com/search?btnI=1&q=msdn+setconsolemode

Hope this helps.
I'm writing a simple console program, I just need it to press enter whenever a number is typed.
/me roll eyes

I'm using plain, simple English.
The user always has to press enter whenever something is typed... otherwise are they not just typing?
http://www.gnu.org/software/libtool/manual/libc/Buffering-Concepts.html

The standard input is always initialized in line-buffered mode. You need to turn it to unbuffered to get a character the instant the user presses it.

The above code turns off line-buffering on Microsoft Windows. If you are using another OS, let me know.
Topic archived. No new replies allowed.