|
-
October 15th, 2010, 08:41 AM
#7
Re: Beginner's use of system("pause");
 Originally Posted by Eri523
Which side effects should that be besides flushing the stream and resetting the error control state?
It's up to debate, but I would say those are side effects yes. IMO, pausing a program and its input are two completely un-related things.
Consider especially that some command-line programs' input stream are not the keyboard, but the raw output stream of other programs (think LAME.exe, x264.exe etc.). The side effect would be the program not stopping, and the stream being completely corrupted.
Of course, those aren't toy programs anymore, but even for a toy program, try this:
Code:
int main()
{
pause();
}
This is what my console looks like:
Code:
Please press enter...
hello world!<return>
What's up?<return>
-------End program
The program ask me to press enter, but it first lets me write an entire sentence because it is trying to flush the stream. Then, I get to write another sentence, before the program validates my input. It feels strange, because I expect to just hit space.
Also, try this:
Code:
int main()
{
std::string name;
std::string surname;
std::string age;
std::cout << "Please type your Name, Surname and age seperated by spaces" << std::endl;
std::cin >> name;
std::cout << "Your name is " << name << "!" << std::endl;
pause();
std::cin >> surname;
std::cout << "Your surname is " << name << "!" << std::endl;
pause();
std::cin >> age;
std::cout << "Your age is " << name << "!" << std::endl;
pause();
}
Am I nitpicking? A bit. Yes. But I find the crusades against system("pause") to be a bit silly.
Is your question related to IO?
Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|