Why `system("pause")' shouldn't be used?

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2009
Posts: 65
Reputation: neithan is an unknown quantity at this point 
Solved Threads: 2
neithan's Avatar
neithan neithan is offline Offline
Junior Poster in Training

Why `system("pause")' shouldn't be used?

 
0
  #1
Oct 8th, 2009
* Don't use system("pause") to pause your program if possible. Use getchar( ) if you are using C and cin.get( ) if you are using C++.
Why is that? A reasonable explanations for those tips would help to learn.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 1
Reputation: ugyen t is an unknown quantity at this point 
Solved Threads: 0
ugyen t ugyen t is offline Offline
Newbie Poster
 
0
  #2
Oct 9th, 2009
getchar()-it shows the character to be displayed on screen. Where else getch() gives the character but doesn't display on the screen.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 65
Reputation: neithan is an unknown quantity at this point 
Solved Threads: 2
neithan's Avatar
neithan neithan is offline Offline
Junior Poster in Training
 
0
  #3
Oct 9th, 2009
Originally Posted by ugyen t View Post
getchar()-it shows the character to be displayed on screen. Where else getch() gives the character but doesn't display on the screen.
Yes but, why don't use system("pause") at all? I mean what's wrong with it?
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,341
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 237
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c
 
1
  #4
Oct 9th, 2009
Originally Posted by neithan View Post
Yes but, why don't use system("pause") at all? I mean what's wrong with it?
http://cboard.cprogramming.com/c-pro...tml#post394525
>system("pause");
This is all kinds of wrong. First, while system is a standard function and thus portable, the argument you pass to it cannot be portable because it relies on the system command interpreter. If you move the code to a different system, the command interpreter will probably be different (or not available!) and system("pause") could do something completely different or break.

Second, system is unsafe because it doesn't verify that the pause program is the one supplied by the system. It may be a malicious program bent on wreaking havoc.

Third, system is sloooooooow. Dreadfully, painfully, agonizingly sloooooooow. This isn't really a problem if you call it once, like here, but system should really be avoided in favor of better alternatives. My code shows one of those alternatives. It requires the user to hit return, but it's portable and does the job admirably with no noticeable inconvenience to the user.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 65
Reputation: neithan is an unknown quantity at this point 
Solved Threads: 2
neithan's Avatar
neithan neithan is offline Offline
Junior Poster in Training
 
0
  #5
Oct 10th, 2009
Although i'm using it only to avoid the cmd from closing without getting a chance to glance the outpot, that was a nice explanation and very usefull.

Thanks a lot!
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 1,968
Reputation: tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute 
Solved Threads: 214
tux4life's Avatar
tux4life tux4life is offline Offline
Posting Virtuoso
 
1
  #6
Oct 10th, 2009
Originally Posted by neithan View Post
* Don't use system("pause") to pause your program if possible. Use getchar( ) if you are using C and cin.get( ) if you are using C++.
Why is that? A reasonable explanations for those tips would help to learn.
Originally Posted by neithan View Post
Yes but, why don't use system("pause") at all? I mean what's wrong with it?
http://www.gidnetwork.com/b-61.html
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 65
Reputation: neithan is an unknown quantity at this point 
Solved Threads: 2
neithan's Avatar
neithan neithan is offline Offline
Junior Poster in Training
 
0
  #7
Oct 13th, 2009
Originally Posted by tux4life View Post
http://www.gidnetwork.com/b-61.html

Awesome, thank you.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 118
Reputation: marco93 is infamous around these parts marco93 is infamous around these parts marco93 is infamous around these parts 
Solved Threads: 12
marco93 marco93 is offline Offline
Junior Poster
 
-4
  #8
Oct 13th, 2009
On Windows, system() is prohibited by MS.
It's dirty, it creates another useless PAS, etc...
Reply With Quote Quick reply to this message  
Reply

Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC