2

I've read that system("pause") is slow and not advisable to use.
Is there any function that I can use instead of that?
I've tried getchar() but if I have a scanf call before, it simply does not waits for an other input, only if I put an other getchar() under it (but I don't think it's a good solution).

edit: I'm using Microsoft Visual Studio 2010

user
  • 6,567
  • 18
  • 58
  • 85
  • What are you trying to achieve? `getchar()` reads input which isn't the same as pausing. What do you need to pause for? – CB Bailey Jul 23 '10 at 22:50
  • 13
    There is something amusing about the complaint that a 'pause' call is slow. – Will Dean Jul 23 '10 at 22:52
  • http://stackoverflow.com/questions/2529617/how-to-stop-c-console-application-from-exiting-immediately http://stackoverflow.com/questions/2725823/how-do-i-get-the-screen-to-pause-closed http://stackoverflow.com/questions/1107705/systempause-why-is-it-wrong – CB Bailey Jul 23 '10 at 22:55

4 Answers4

2

I've tried getchar() but if I have a scanf call before, it simply does not waits for an other input

Make sure you empty the input buffer before calling it; otherwise it might grab a key that was already in the buffer (like, say, a newline character...).

Amber
  • 507,862
  • 82
  • 626
  • 550
0

Have you tried?

  • sleep(int time);
  • getch();
Kristina
  • 15,859
  • 29
  • 111
  • 181
0

_getch() from conio.h should prove an adequate replacement.

ChrisV
  • 3,363
  • 16
  • 19
0

That system("pause") call is inserted in autogenerated code by some IDEs (Bloodshed Dev C++ comes to mind) because the console window that pops up when you run the code from the IDE is ephemeral and disappears as soon as the program is done.

The solution is to not run the code from the IDE--open a real console window instead, and you'll still be able to read your program's output once it terminates.

Drew Hall
  • 28,429
  • 12
  • 61
  • 81