0

Please, is there a way I can wait for a single ENTER key press in all of the below cases? It works fine except when the std::getline() is used, which consumes the \n, and the ENTER needs to be pressed twice.

The code below works fine:

#include <iostream>
#include <limits>

using std::cout;
using std::cin;
using std::string;

void waitForENTERKeyPress();

int main ()
{
    char c;
    string s;

    cout << "Enter c:"; cin >> c;
    waitForENTERKeyPress();

    cout << "Enter c:"; c = cin.get();
    waitForENTERKeyPress();

    cout << "Enter s:"; cin >> s;
    waitForENTERKeyPress();

    cout << "Enter s:"; std::getline(cin, s);
    waitForENTERKeyPress();

    return 0;
}

void waitForENTERKeyPress()
{
    // Does not work with std::getline(...)
    cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n');
    cout << "Press an ENTER to continue...\n";
    cin.get();    
}

====[UPDATE]============================================

Let me rephrase what I am looking for: The way how to make the waitForENTERKeyPress() function working in both cases – with \n in the buffer, when cin operator >> or cin.get() is used and also in cases the std::getline() is used.

From what I understand it is not possible as there is not a way how to detect if the \n is still in the buffer or not. I have tried to use cin.rdbuf()->in_avail(), but it always returns 0.

STeN
  • 51
  • 3
  • Possibly related: [Why does std::getline() skip input after a formatted extraction?](https://stackoverflow.com/q/21567291/11082165) – Brian61354270 Apr 05 '22 at 19:20
  • >> This question already has answers here: Why does std::getline() skip input after a formatted extraction? NO IT HAS NOT. I know why it happens... I am asking how to solve it :( – STeN Apr 05 '22 at 19:27
  • I also disagree with the duplicate because this is the reverse. @STeN, there is but one way: remove the unwanted line-ending after the operation that leaves the unwanted line ending, not before some future operation that may-or-may not have an unwanted line ending in the stream. `cin.ignore( std::numeric_limits::max(), '\n');` will always ignore to the next line ending. If there is no line ending, it'll wait for one to be input. – user4581301 Apr 05 '22 at 19:52
  • 1
    Since `std::getline()` consumes the `\n`, simply DON'T call `waitForENTERKeyPress()` after it, since there is nothing to wait on. Same with `cin.get()` if it returns `\n`. That is not the case when using `operator>>`, hence the need to call `waitForENTERKeyPress()` after it. – Remy Lebeau Apr 05 '22 at 22:16
  • 1
    To solve this, take `cin.ignore` statement out of the `waitForEnterKeyPress` function , and call it when needed. The function as it stands is poorly titled as it does two separate things: remove a previous `\n`, and wait for enter key press. Now that the situation arose where you need to do only one of those two things, you see why that was poor . – M.M Apr 05 '22 at 22:22
  • Thanks all for the comments - I was naively looking for such a variant of `waitForENTERKeyPress()` that will work in both cases – with `\n` in thr buffer (when `cin` operator `>>` or `cin.get()` is used and also in cases the `std::getline()` is used) – that seems not to be possible as there is not a way how to detect if the `\n` is still in the buffer or not. I have tried to use `cin.rdbuf()->in_avail()`, but it always returns 0. – STeN Apr 07 '22 at 04:27
  • It isn't clear what you expect the solution to do. Make `std::getline()` read a line up to but not including a newline character? It won't do that. – n. m. could be an AI Apr 08 '22 at 11:56

0 Answers0