In windows, the system() function is used to execute any operating system command, like dir, ls, rename, etc. However, it is not encourage to use system("pause") to pause command windows from closing in C++. There are two main reasons why:-
- A call to system("pause") will pause the programs execution and make a system call and thus allocate unnecessary resources.
- Another reason it should be avoided is because it isn't portable. It works only on systems that have the PAUSE command at the system level, like DOS or Windows.
system("pause") is pretty popular and usually introduced to new beginners. It provides a clean and perfect pausing action. Also commonly used in debugging.
That means, when you do system("pause"), it does: Press any key to continue...
Just ANY key on your keyboard! Even an arrow or a space bar will do.
Unlike cin.get(), it didn't show anything, or what you suppose to press to exit. Until you figure it out, you probably hit almost every key on your keyboard, and finally accidentally hit on the RETURN key.
Of coz, both code: system("pause") and cin.get() does the same job, but quoting from a friend, using system("pause") is like using a bulldozer to open your front door. It works, but you can open your front door in an easy way, do you? So, why would you want to waste the unnecessary resources?
In spite of all the bad things most programmer would said about system("pause"), and how you shouldn't mix an operating system command with a C++ programming code, I reckon system("pause") is introduced to new beginner to make their life easier in learning and exploring programming language. Just like how we were taught that gravity is equal to 10 when we were in primary school, and then few years later they told us that actually we should calculate gravity as 9.81.You wouldn't tell a little boy that he should calculate gravity as 9.81 while he is still struggling with multiplication involving decimal, would you??
So, it's your call if you wanna be a beginner and stay as a beginner, or evolve and be a professional =)
No comments:
Post a Comment