Dart
Dart

Reputation: 11

C++ - Any way to insert an enter into the buffer/Stop cin.ignore requiring enter?

Pretty much as the title says. I'm creating a code segment and need to clear the buffer before it in order to start a new input, however, when using cin.ignore(), the buffer is cleared but the user is required to hit another enter afterward. Is there any way I can insert an enter into the buffer to automatically hit enter for the user, or that I can get cin.ignore to not require another enter.

I am using cin.getline to retrieve the input.

void Clear(){
    cin.clear();
    cin.ignore(250, '\n');
}

Upvotes: 1

Views: 357

Answers (1)

Zig Razor
Zig Razor

Reputation: 3495

2 issues:

  • The first parameter in the "std::cin.ignore()" that you are using just comes down to a very large number. This should be the maximum number of characters that the input buffer can hold. This number may be different on different systems or even header files for different compilers.

  • You need to press enter twice because there is nothing in the buffer to ignore. It is waiting for something to be entered to ignore. some people will use this to pause the program before the "return 0;".

Upvotes: 1

Related Questions