|
||||
|
« 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 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. Reasons:
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 windowShould 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 wordThese 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:
|
GIDNetwork Sites
Archives
Recent GIDBlog Posts
Recent GIDForums Posts
Contact Us
|
« What are Google Sitemaps good for? | Conversion: Binary, Decimal, Hexadecimal » |