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
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
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
18,528 posts since Apr 2005
Reputation Points: 1,826
Solved Threads: 609
Skill Endorsements: 29
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
It depends on what preceeded the cin.get().
Ancient Dragon
Achieved Level 70
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
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
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
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
11,404 posts since May 2006
Reputation Points: 3,421
Solved Threads: 1,055
Skill Endorsements: 37
Use getline() or some equivalent function that reads an entire line.
WaltP
Posting Sage w/ dash of thyme
11,404 posts since May 2006
Reputation Points: 3,421
Solved Threads: 1,055
Skill Endorsements: 37