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' );
}
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>>.