Aztec warrior
Aztec warrior

Reputation: 183

program exits automatically

I have created a simple .exe file. When I run it, it closes automatically instead of saying "press any key to exit".

This is my program:

#include <iostream>
#include <string>
using namespace std;
int main()
{
  string answer, yes = "yes";  
  cout << "Is Lucy a top lass ? enter yes or no" << endl;
  cin >> answer;
  if (answer == yes)
  {
    cout << "Correctomundo" << endl;
  }
  else
  {
    cout << " Blasphemy ! " << endl;
  }
  return 0;
}

How do I make it ask the user to press any key before exiting?

Also is there any way I can change it so it says something else instead of "press any key to exit"?

Upvotes: 0

Views: 284

Answers (2)

Dao Xio
Dao Xio

Reputation: 148

Try putting system("pause"); before the return 0;

Upvotes: 0

Paul Virally
Paul Virally

Reputation: 156

Add std::getchar(); at the end of your code right before return 0;. This will make the program wait for you to type in a character from standard input (the keyboard) and press enter (or just press enter) before it exits. (You might need to #include <cstdio> at the beginning for it to work though)

If you want to get really hacky, you can put a breakpoint (you can learn how to do that here) at the end of your code but that will only work when you are in debugger mode, not in release mode...

Upvotes: 1

Related Questions