0

I tried std::getchar();, cin::get(); and system ("sleep");, but nothing stops the console from closing, because it seems, that all of these functions misinterpret the pressed enter key that was supposed to confirm input for scanf. How can I prevent the console from closing with a "Press enter / any key to close" behavior after scanf? I don't want to use functions stopping the console totally from doing something for some time (like sleep) or non-portable functions (like system ("sleep")), unless such functions are the only ways.

int main () {
    wchar_t *user = new wchar_t[30];
    wscanf (L"%30ls", user);
    // Process data... (very short time)

    std::getchar ();

    return 0;
}

IDE: Visual Studio 2013 (12.0) Express I don't know the compiler. I created an empty C++ project and didn't change any settings.

Cubi73
  • 1,891
  • 3
  • 31
  • 52

2 Answers2

1

Do it like this:

// do your stuff here

// prevent console from closing
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cin.get();
char8_t
  • 320
  • 2
  • 8
  • Because this answer works as well as merlin's solution (using `getchar()` twice, you get the correct answer. – Cubi73 Aug 06 '14 at 20:24
-1

Use system("pause"); at the end of main() function.

Subodh S
  • 105
  • 1
  • 4