Search code examples
c++inputcharentergetch

C++ How check characters in real time with _getch()


i need in my loop get character in realtime and check it by conditions. If user press whatever except enter, program works fine. Can anyone help me ? thanks !

   while (read != '\n')
            {
                cout << "Enter character:\n";
                read = _getwch();
                if (read == '\n') {
                    cout << "You pressed : ENTER\n";
                }
                else {
                    cout << "Your character is: \"" << read << "\"\n\n";
                    read = '\0';
                }
            }

Solution

  • include

    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;

    }

    This code will help in detecting the ENTER key when pressed. Hope this helps you.