+ 1

How can I check that user press ENTER key in c++ ??

I want to preempt the user if user press ENTER key user will be able to continue further process if user not than the program will terminate I NEED GUIDE I NEED HELP

28th Jun 2019, 4:36 AM
Nouman Bin Sami
Nouman Bin Sami - avatar
6 Réponses
+ 4
Checking the input character by character is the easiest way to determine that. int main() { int n; cout << "Press Enter to proceed...\n"; while ( n = cin.get() ) { if ( n == (int)'\n' ) { cout << "Carriage return and line feed encountered\n"; break; } else { cout << "Program is terminating...\n"; exit(EXIT_FAILURE); } } // rest of the program... } Sample input 1: Press Enter to proceed... dsg564sd6g46 Program is terminating... Sample input 2: Press Enter to proceed... Carriage return and line feed encountered
28th Jun 2019, 5:27 AM
To Seek Glory in Battle is Glorious
To Seek Glory in Battle is Glorious - avatar
+ 2
use getch()..... the output screen will wait for a key to enter(preferably Return) P.S. you can try this format but I am afraid it would work char ch ; cin>>ch ; if(ch=='\r') cout<<"User entered Return key" ;
28th Jun 2019, 4:44 AM
Aditya
Aditya - avatar
+ 1
Maggie Smith I think this will work
28th Jun 2019, 5:17 AM
Nouman Bin Sami
Nouman Bin Sami - avatar
+ 1
" I think instead of cin.get() we can use getch() its better" You're totally wrong. `getch()` is a deprecated function from the long-dead header file <conio.h> in "C" language which no longer being used (and supported) for getting single characters. Instead, as your question tag emphasizes, you should look for the equivalent solution in C++. Also, the adjective "better" is a relational term. A "better" solution for problem A might be a "worse" solution for problem B.
28th Jun 2019, 3:47 PM
To Seek Glory in Battle is Glorious
To Seek Glory in Battle is Glorious - avatar
+ 1
You might be right, but bro I need just a single character so therefore I thought it would be better
28th Jun 2019, 3:53 PM
Nouman Bin Sami
Nouman Bin Sami - avatar
0
To Seek Glory in Battle is Glorious bro I think instead of cin.get() we can use getch() its better ☺
28th Jun 2019, 3:07 PM
Nouman Bin Sami
Nouman Bin Sami - avatar