Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.
Trending News
how do I end a c++ main program so that the program waits for a key stroke before exiting?
I'm using MS Visual C++ and using type "return 0" at the end of my main program. When i run the compiled program, I want the DOS screen to stay open until I hit 'enter' or any other key.
5 Answers
- HexLv 51 decade ago
Don't use a crutch such as the system() function.
#include <iostream>
#include <conio.h>
using namespace std;
int mai()
{
cout << "Test";
_getch();
cin.get();
return 0;
}
both _getch(); and cin.get(); will wait for 'Enter' or 'Return' to be pressed before continuing with the execution of your code. If you're looking to terminate the application then use exit(); like:
_getch();
exit( 0x004 );
will wait for 'Enter' to be pressed and will exit the application with the termination code of 4.
Words of Wisdom,
- Hex
- [ J ] a [ Y ]Lv 41 decade ago
there is a disadvantage to using getchar() over system("PAUSE");
getchar will wait for the ENTER key to be pressed before continuing, whereas system("PAUSE") waits for any key at all.
- How do you think about the answers? You can sign in to vote the answer.
- 1 decade ago
You have two options:
system("pause");
which is the dirty way to do it, or:
getchar() ;
system("pause"); is dirty because it's a system call, and is dependant on the operating system.
getchar(); is a C/C++ function that runs on any system (Mac/Linux etc.)