Detecting Enter Key?

Hello,

How would you make a program detect when the user enters the enter key. I don't want the exact answer, just a hint to nudge me in the right direction.

Thanks.
If it's a console app then using 'cout.get()' will wait untill the enter key is pressed
I've tried that and it's not working. Any other suggestions?
http://cplusplus.com/forum/beginner/1988/

There are some good tips in here for keeping the console open, not sure if it's specifically aimed to the enter key or not.
I think I've found what I've been looking for :

1
2
cout << "Press ENTER to quit.";
  cin.ignore( numeric_limits<streamsize>, '\n' );


Thanks.

EDIT: This code doesn't work either. How do you do it!?
Last edited on
Any other suggestions for making cin detect when a user inputs the Enter Key.
Have you tried cin.get()?
Thanks for your reply. I'll see if it works...

:|
http://www.cplusplus.com/forum/beginner/2624/

apparently the cin.get() works

1
2
cout << "Press the ENTER key";
  if (cin.get() == '\n')
Here it is. Very simple:

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
#include <iostream>
#include <cstring>
#include <cmath>

using namespace std;


double get_key();


int main(){
    
    
    double n;
    
    
    
    while(true){
                
                cout << "Input number to get square root: "<< endl;
                
                n = get_key();
                
                if(n == 0.0){
                     break;
                     }
                     
                     cout << "Result: " << sqrt(n) << endl;
                     
                     }
                     
                     system("PAUSE");
                     return 0;
                     
                     }
                     
                     
                     double get_key(){
                            char lenght[100];
                            
                            cin.getline(lenght, 100);
                            if(strlen(lenght) == 0.0)
                                          return 0;
                                          return atof(lenght);
                                          
                                          }
                            
                           
Yep, It works! Thanks a lot guys.
Topic archived. No new replies allowed.