Multiple anti-closers having to be used.

I have looked around at that common issue beginners tend to ask a lot - "How do I stop the console closing down?", finding many ideas. The simplest always seems to be something along the lines of "use getchar(); or std::cin.get(); just before return", which used to work for me on my old laptop.

Now that I am using a powerful PC that I built, for some reason I am having to put these lines at least twice before it prevents closing. Is there something I am completely missing that I have forgotten from when I used the laptop?

Here is a really simple example of what I have used at the end of the main function:

1
2
3
        std::cout << "\n\n\nPress [enter] to exit.";
        getchar();
        return 0;


The above will not prevent closure, but the following will (enter still only has to be pressed once):

1
2
3
4
        std::cout << "\n\n\nPress <enter> to exit.";
	getchar();
        getchar();
	return 0;



I am using VS 2013 if that makes any difference to anything.

For people who will say "use CTRL-F5", I was hoping to have the same effect in the actual application.

Apologies for any blatant newby mistakes, and thanks in advance!
Thanks for the link - That's pretty much what I tend to do instead now anyway, I was more wondering as to why this technique now only works after two lines unlike before, where only one was adequate - or if it was me just remembering wrong and making a silly mistake.
closed account (z05DSL3A)
The usual mistake is not clearing out the input stream.
Bearing in mind I probably used inputs a lot less before, that does make sense. Thanks for the help.
Just noticed this is also happening for

1
2
3
        std::cout << "Press <enter> to continue...";
	std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
        return 0;


Do I need to add anything else?
Scrap that, complete numpty moment, didn't #include <limits>

...
Topic archived. No new replies allowed.