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

Recommended Answers

All 16 Replies

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

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

Ah Ok. Thanks for the help :)

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

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

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.

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

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.

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

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.

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

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.