detecting the EnterKey press with cin

hi
i am trying to detect the newline key(ENTERKEY) press.please point out the error in the foll pgm.

1
2
3
4
5
6
7
8
9
10
11
int main()
{
	while (1)
	{
		int a;
		cin>>a;
		if (a=='\n')
			cout<<"you have pressed Enter key";
	}

}

The error is that you can't do it like that.

The >> operators are for inputting formatted text. You must either use low-level input functions (like cin.get()) or input text with ENTER in mind:
1
2
3
4
5
6
7
8
9
10
11
12
13
// low-level.cpp
#include <iostream>
using namespace std;

int main()
  {
  cout << "Press the ENTER key";
  if (cin.get() == '\n')
    cout << "Good job.\n";
  else
    cout << "I meant ONLY the ENTER key... Oh well.\n";
  return 0;
  }

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// intelligent-input.cpp
#include <iostream>
#include <map>
#include <sstream>
#include <string>
#include <vector>
using namespace std;

int main()
  {
  vector <pair <int, int> > pairs;
  string s;
  int a, b;

  cout << "Enter a list of pairs of integers, one pair per line, as\n  1 2\n"
          "Press ENTER on a blank line to finish.\n\n> ";

  // While there is input and the user has not simply pressed ENTER
  while (getline( cin, s ) && !s.empty())
    {
    stringstream ss( s );
    ss >> a >> b;
    if (!ss)
      cout << "Invalid pair. Try again";
    else
      pairs.push_back( make_pair( a, b ) );
    cout << "> ";
    }

  // Now get a single number from the user, which we'll use to search the list
  cout << "\nThank you. Now enter a number to find> ";
  while (getline( cin, s ))
    {
    if (stringstream( s ) >> a) break;
    cout << "Invalid integer. Try again> ";
    }

  // Count the number of times the user's number appears in the list
  b = 0;
  for (vector <pair <int, int> > ::iterator p = pairs.begin(); p != pairs.end(); p++)
    {
    if (p->first  == a) b++;
    if (p->second == a) b++;
    }

  switch (b)
    {
    case 0:  cout << "That number does not appear in the list.\n";
    case 1:  cout << "That number appears in the list 1 time.\n";
    default: cout << "That number appears in the list " << b << " times.\n";
    }

  cout << "\nThanks for playing. Bye!\n";
  return 0;
  }

Both examples work because user input is line buffered, meaning that the user must press ENTER before your program will see any input at all. That is also how users expect the program to behave.

Hope this helps.

[edit] BTW, the examples are larger than they needed to be. I've provided complete programs you can play with. For the second example, the points of interest are on lines 19..28.
Last edited on
Thanks Duoas
Hope this helps

very much

the second program was great.
you can use cin.get in your program as follows
int main()
{
while (1)
{
int a;
cin>>a;
if (a==cin.get)
cout<<"you have pressed Enter key";
}

}
i get compile errors before replacing cin.get with cin.get().
where in u r program do u check the Enter Key press.
His program doesn't work (and can't, to detect ENTER).
Topic archived. No new replies allowed.