My programs closes after its execution...

I write a program, and it closes itself after its execution.
I use the "cin.get();", the program should expect the user to type a key, but it doesn't works. So I have to use the "system("PAUSE")" but I prefer using cin.get();....
If you post the code here I am sure I can have a look at it and see how you would go about using the cin.get(); method of pausing.
It would be better if you can post the block of code that you have written.
So that it will help in knowing the problem better.
Last edited on
The problem that you might have is that there are still characters in the input buffer when the command is given. To fix this, you could put an ignore command right before cin.get() which will not pause if there are still characters to be read.
1
2
 cin.ignore (BUFSIZ, '\n');
cin.get();

Having not seen the code, this is my best guess...
Another option is to just declare a variable and then just cin that variable right before returning in your main function. I have seen people do the following:

1
2
3
4
5
6
7
int main()
{
     //blah blah blah
     int closer;
     cin >> closer;
     return 0;
}


Granted you'll be using a few bytes of memory. :P
Here is a good way to pause in C++:

1
2
3
cin.clear();  // Ensure error-free stream.
cin.sync();   // Empty buffer.
cin.get();    // Pause for Enter key 
Topic archived. No new replies allowed.