+ 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
6 odpowiedzi
+ 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
+ 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" ;
+ 1
Maggie Smith I think this will work
+ 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.
+ 1
You might be right, but bro I need just a single character so therefore I thought it would be better
0
To Seek Glory in Battle is Glorious bro I think instead of cin.get() we can use getch() its better ☺