Programme closes

Feb 10, 2014 at 11:59am
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;
}
Feb 10, 2014 at 12:02pm
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.
Feb 10, 2014 at 4:17pm
add getch() just before return 0;
So it will wait for your enter to terminate.
Feb 10, 2014 at 6:32pm
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;
} 
Feb 11, 2014 at 10:37am
It worked. Thank you so much everyone.
Topic archived. No new replies allowed.