user
user

Reputation: 6797

C++ Replacing system("pause") call

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

Upvotes: 2

Views: 1622

Answers (4)

Drew Hall
Drew Hall

Reputation: 29047

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.

Upvotes: 0

ChrisV
ChrisV

Reputation: 3423

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

Upvotes: 0

Amber
Amber

Reputation: 526573

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...).

Upvotes: 2

krb
krb

Reputation: 16315

Have you tried?

  • sleep(int time);
  • getch();

Upvotes: 0

Related Questions