Program keeps shutting off when I press a button
So I started learning how to use C++ yesterday and I forget how to do something basic. I used to know how to do this, but I forget. So whenever I start this up it shows up. If you press either 1 or 2 (which is supposed to show you text), it shuts down. So how do I prevent this from shutting down? The code I already wrote is below and I use Dev-C++.
If you haven't noticed I'm trying to make an text adventure game. So if I did anything wrong tell me how it's supposed to be done so I don't write paragraphs upon paragraphs of code that won't work how it's supposed to work.
Code:
#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
int checkoutknights;
cout<<"After the long, failed journey to pursue the Black Monster,\nyou're finally ready to go home. You're in a forest and decided\nto rest and read a bulletin. It reads 'change of war more likely'.\nSuddenly you hear a group of knights galloping on their horses\na few meters ahead. What do you do?\n\n1: Go check it out.\n2: Stay here and ignore them.\n";
{
if (checkoutknights == 1)
cout<<"You go and check out the knights only to see that they already left.\nYou decide to go back when two knights behind the group\nconfront you.\n";
}
{
if (checkoutknights == 2)
cout<<"You stay where you are and ignore the knights. Eventually you stop hearing them.\n";
}
cin.get();
}
I figured it out on my own
Well thanks for helping me -_-. I rewrote the code and now it works.
Code:
#include <iostream>
using namespace std;
int main()
{
int answer;
cout<<"After a long, failed journey to pursue the Black Monster, Dart decides\nto go home. He's only a few miles away from how and decides to take\n his last break in the nearby forest. He reads a bulletin he had with him saying:\nChange of war more likely. He then hears a sound, the sound\nof galloping horses. What should he do?\n\n1: Go check it out\n2: Ignore it\n";
cin>> answer;
cin.ignore();
if ( answer == 1 ) {
cout<<"Dart stood up to go check out the horses. When he arrives, they were\nalready gone.Then he got confronted with two knights who were further away from the group Dart just missed.\n";
}
if ( answer == 2 ) {
cout<<"Dart ignores the fading noises until it went away.\nA few minutes later he stood up and continued his journey home.\n";
}
cin.get();
}