Please break it down for me?

So i was told to place this in my program to stop the command window from closing

std::cout << " Press ENTER to continue...";
std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );

It is working great, but i was wandering if somebody could break this down for me so that i can understand how and why it works?
(updated since 1st response)
Now that i've added a second variable that the user defines, this isn't working anymore. How would i tweak this so that it would work for this circumstance?

heres the full program:

#include <iostream>
using namespace std;

int main ()
{
int a,b;
a = 5;
cin >> b;
a=a-b;
cout << a;
std::cout << "Press ENTER to continue...";
std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );
}
Last edited on
I'm assuming you understand the first statement...

As for the second: http://www.cplusplus.com/reference/iostream/istream/ignore/
std::numeric_limits<std::streamsize>::max() returns the size of the buffer that the stream is using.
Last edited on
Never mind. I used system("PAUSE") in place of those 2 lines of code and it is working
No, don't do that! System commands are making a call directly to your operating system, which is a slow and demanding process. Not only that, but they aren't platform dependent! http://www.gidnetwork.com/b-61.html
Now that i've added a second variable that the user defines, this isn't working anymore. How would i tweak this so that it would work for this circumstance?

This is happening because the call to cin>> is leaving a newline in the stream. Try your original code with a call to cin.sync(); after the call to cin>>.
You don't need to std:: all of those times; you're already using the namespace.

Doesn't endl purge the buffer? It might work if you change cout << a; to cout << a << endl;
Last edited on
Doesn't endl purge the buffer?

Nope.
Topic archived. No new replies allowed.