Programme closes

My programme is working just fine but when I press enter after entering the game. It shows the last cout but closes imidiately. What to do?

#include <iostream>
#include <string>

using namespace std;

int main()
{
string name = "";
int age = 0;

cout << "Enter your name.\n";
cin >> name;

cout << "Enter your age.\n";
cin >> age;

cout << "You name is:" << name << ".";
cout << "You age is:" << age << ".";

return 0;
}
Run the program from the command line, or change the settings in your IDE to avoid that the window close after the program has terminated.
add getch() just before return 0;
So it will wait for your enter to terminate.
Like ak16 said use _getch.You can find it in conio.h .Not all compilers have it but as i know most popular ones : GNU GCC and VC++ have it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <string>

using namespace std;
#include <conio.h>
int main()
{
string name = "";
int age = 0;

cout << "Enter your name.\n";
cin >> name;

cout << "Enter your age.\n";
cin >> age;

cout << "You name is:" << name << ".";
cout << "You age is:" << age << ".";
_getch();
return 0;
} 
It worked. Thank you so much everyone.
Topic archived. No new replies allowed.