We're a community of 1.1M IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,080,644 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

system("pause")

I've read that one should use cin.get() instead of system("pause"). Why is that?

8
Contributors
16
Replies
4 Years
Discussion Span
9 Months Ago
Last Updated
17
Views
Azurea
Light Poster
38 posts since Oct 2007
Reputation Points: 10
Solved Threads: 4
Skill Endorsements: 0

1) system("pause") is not portable -- only works on MS-Windows and MS-DOS operating systems.

2) cin.get() is a lot faster

Other reasons

Ancient Dragon
Achieved Level 70
Team Colleague
32,269 posts since Aug 2005
Reputation Points: 5,852
Solved Threads: 2,590
Skill Endorsements: 70

system("pause") represents an unsafe programming practice. Not only is the argument to the system function non-portable (your code would work on Windows but not Linux, for example), someone could replace the "pause" program on the machine with a malicious program and use it to cause trouble. Spoofing a legitimate program is a common practice among malware.

>2) cin.get() is a lot faster
That's kind of a lame excuse. It makes sense for other uses of system, but who cares how fast it is when the point of the operation is to pause the program long enough for a user to read the output? :icon_rolleyes:

Narue
Bad Cop
Team Colleague
15,460 posts since Sep 2004
Reputation Points: 6,483
Solved Threads: 1,407
Skill Endorsements: 55

Spoofing a legitimate program is a common practice among malware

yeah, i heard about thinks like that. e.g people replacing "ls" in linux with a command to recurisvely delete all directories. Will teach you to run as root ;)

jbennet
Moderator
Team Colleague
18,528 posts since Apr 2005
Reputation Points: 1,826
Solved Threads: 609
Skill Endorsements: 29

Ah Ok. Thanks for the help :)

Azurea
Light Poster
38 posts since Oct 2007
Reputation Points: 10
Solved Threads: 4
Skill Endorsements: 0

But I've found that when there were "cin" statement(s) earlier in the program, then cin.get() didn't work, only system("pause") worked. Did others encounter the problem as well?

sgw
Junior Poster in Training
66 posts since Jan 2008
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Yeah, I get that same problem, except I normally put it at the end of main().

Azurea
Light Poster
38 posts since Oct 2007
Reputation Points: 10
Solved Threads: 4
Skill Endorsements: 0

It depends on what preceeded the cin.get().

Ancient Dragon
Achieved Level 70
Team Colleague
32,269 posts since Aug 2005
Reputation Points: 5,852
Solved Threads: 2,590
Skill Endorsements: 70

Just this simple program:

main()
{
int x;
cout << "enter x: ";
cin >> x;
cout << x;
cin.get();
return 0;
}

Using the Dev-C++ the output won't stay on screen.

sgw
Junior Poster in Training
66 posts since Jan 2008
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Do cin.get() twice. Read the FAQs and stickies for more info.

Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
Skill Endorsements: 39

Thanks, doing it twice does solve the problem. So under what circumstances would doing it once solve the problem? What's the difference? I checked FAQ and didn't find answer--if you could point out a source would appreciate it! just curious when to do it once and when to do it twice.

sgw
Junior Poster in Training
66 posts since Jan 2008
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

See post #17 here, which is the identical problam that you have.

Ancient Dragon
Achieved Level 70
Team Colleague
32,269 posts since Aug 2005
Reputation Points: 5,852
Solved Threads: 2,590
Skill Endorsements: 70

Thanks for the link. I just read your post there. So it looks like unless you use cin.ignore(), you would always need to type cin.get() twice. There's no cleaner way...

sgw
Junior Poster in Training
66 posts since Jan 2008
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Thanks, doing it twice does solve the problem. So under what circumstances would doing it once solve the problem? What's the difference? I checked FAQ and didn't find answer--if you could point out a source would appreciate it! just curious when to do it once and when to do it twice.

Yes, I suppose I should have specified an FAQ or two.

Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
Skill Endorsements: 39

Thanks for the link. I just read your post there. So it looks like unless you use cin.ignore(), you would always need to type cin.get() twice. There's no cleaner way...

Yes, there are cleaner ways. First is to not use cin . Read a full line into a char* or a string and [search]parse[/search] the line to make sure the input was entered properly.

WaltP
Posting Sage w/ dash of thyme
Team Colleague
11,404 posts since May 2006
Reputation Points: 3,421
Solved Threads: 1,055
Skill Endorsements: 37

WaltP.... example code of this? For us beginners please. I am in a c++ class, run FreeBSD and though I have MonoDevelop, I'd like these basic programs to run and compile properly on Windows (visual studio used at school), Unix (FreeBSD especially, which should mean OS X as well) AND Linux. In otherwords I like standards....

ikbendeman
Newbie Poster
1 post since Aug 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

Use getline() or some equivalent function that reads an entire line.

WaltP
Posting Sage w/ dash of thyme
Team Colleague
11,404 posts since May 2006
Reputation Points: 3,421
Solved Threads: 1,055
Skill Endorsements: 37

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page generated in 0.1102 seconds using 2.76MB