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

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:

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 ;)

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?

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

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.

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.

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...

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.

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.

Edited 4 Years Ago by WaltP

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....

This article has been dead for over six months. Start a new discussion instead.