How does this prevent VS console from shutting down?

Write your question here.

Hi guys,

I have some experience in Java and VB.NET from high school. I'm now an EE major and I'm taking my intro computer science class using C++. My teacher recommends DevC++ but I've always liked Visual Studio. So I'm using VS Exp 2012.

I found this line on another site and I'm curious as to how to works to prevent the window from closing. How do the two functions ignore and get work? Nothing happens if I don't have one or the other.

 
std::cin.ignore().get();


Thank you so much!
That's not even the correct way to do what it is trying to do, so there is no telling how or why it works at all ;)

Read the first two posts here:
http://www.cplusplus.com/forum/beginner/1988/
Last edited on
If you call "ignore()" on an std::istream object without passing it any arguments it will throw out the first character it finds in the stream, it technically sets its delimiter to EOF but since it's only getting rid of one character that doesn't really matter. When "ignore()" is done it returns the instance of the std::istream that called it. The "get()" member function is then called against the instance of the stream that "ignore()" returned, and it waits for you to feed it another character.

That's how this code is working anyway, but console programs should be run from the command prompt. So there is actually no need for this kind of nonsense.


EDIT: This doesn't ensure that the input stream is empty after it is called by the way. If you're doing any input after this you may want to ensure that everything gets dropped before that point with "std::cin.sync()" or something else.
Last edited on
Topic archived. No new replies allowed.