GIDNetwork > Pausing a Program
« What are Google Sitemaps good for? Conversion: Binary, Decimal, Hexadecimal »

Pausing a Program

by: WaltP - Aug 24, 2005

Many new programmers that use an IDE have problems when they run their program and a window opens and closes quickly. The problem is the IDE launches the program and the program runs in a console window. This window opens, the program runs and the window closes because it's no longer needed.

The question is How can I see the results?.

We'll explore various ways to do this; some terrible, some good, many controversial. Keep in mind these are my opinions and as such you may take them or leave them. But I will explain my reasoning as we go.

1. system("pause");

Should I use it?: Absolutely not!

We'll start with the worst of all suggestions I've seen, the command system("pause");.

Many people, instructors included, for some inexplicable reason think that making a call to the operating system to run a system command to temporarily halt a program is a good thing. Where they get this idea is beyond me.


  • It's not portable. This works only on systems that have the PAUSE command at the system level

  • It's a very expensive and resource heavy function call. It's like using a bulldozer to open your front door. It works, but the key is cleaner, easier, cheaper. What system() does is:

    • suspend your program

    • call the operating system

    • the OS must now find the PAUSE command

    • allocate the memory to execute the command

    • execute the command and wait for a keystroke

    • deallocate the memory

    • exit the OS

    • resume your program

    There are much cleaner ways included in the language itself that make all this unnessesary.

  • you must include a header you probably don't need: stdlib.h or cstdlib

So please, stay away from this command. It really isn't necessary and it's bad conceptually. I've been programming for over 30 years and only heard of this command 3 years ago. Believe me, I've never needed it in those 30 years, you certainly don't either. It's a bad habit you'll have to break eventually anyway.

2. getch();

Should I use it?: Probably not

getch() is less portable than the system("pause"); command. It is only defined in specific compilers (Borland and MSVC++, Dev C++ kind of) so should be avoided. It is defined in conio.h, another header with limited use and portability. getch() has it's uses, but pausing a program is not one of them. For this functionality, stick with the C standard functions. For C++ you should not even consider this function. It may conflict with other I/O methods used in the C++ program.

3. getchar();

Should I use it?: for C, Yes

This function is defined in all implementations of C. And since it's part of the language itself, it is one of the best solutions. Once your program is built, the function is part of the program and the call simply jumps to the function. Compare that with system("pause");...

Other I/O functions that accept information from stdin and defined in stdio.h can work, but getchar(); is probably the easiest and best.

4. cin.get();

Should I use it?: for C++, Yes

This method is defined in all implementations of C++. For the same reasons as getchar() above, cin.get() should be your pause of choice in C++. Like C, C++ has many methods that can be used.

5. Run from a console window

Should I use it?: Yes

Basically this technique will run the program as it was meant to run -- in a console window (CW). Open a CW and navigate to the directory (folder) the program file is built in. Use the IDE to build the program, switch to the window and run it. You won't need any kludge to pause the program before exiting.

A final word

These are certainly my opinions and should be taken as such. If you have a good argument against these suggestion, please let me know. I'd like to see your reasoning. But if I see system("pause"); in your code, I may mention it... Don't take it personally :wink:

Would you like to comment? This story has been viewed 17,416 times.
« What are Google Sitemaps good for? Conversion: Binary, Decimal, Hexadecimal »


Copyright © GIDNetwork™ 2001 - 2024

Another website by J de Silva

Page generated in : 0.00590 sec.