Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

command prompt closes!

Status
Not open for further replies.

abone114

Programmer
Jul 14, 2005
1
US
hi, im brand new to programming with c++ and i ran into a problem. whenever i execute a program, the command prompt either pops up and disapears in an instant, or it closes whenever i press the enter key... is there anyway to solve this??
 
This is a very common problem. A search will get you lots of answers.

My favorite solution is to add cin.get() as the last line of the main program (but before any return statement). If you are doing user input in your program, you might need to ignore a newline entered by the user, so add this:
Code:
#include <limits>

// code

std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::cin.get();

return 0;
 
Another possible case: if you want to suspend a program to read its console window before termination, add in the main() before return stmt:
Code:
#include <cstdlib>
...
int main()
{
...
   system("pause");
   return 0;
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top