Thread: Program keeps shutting off when I press a button

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    10

    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();
    }

  2. #2
    Registered User
    Join Date
    Jan 2009
    Posts
    10

    Unhappy 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();
    }

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Dude, this is not your taxi express line. People respond when and if they have time.
    And FYI, http://apps.sourceforge.net/mediawik...n_input_buffer
    Last edited by Elysia; 01-11-2009 at 03:51 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How can i made vectors measuring program in DevC++
    By flame82 in forum C Programming
    Replies: 1
    Last Post: 05-07-2008, 02:05 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  3. Emulating button press.
    By System_159 in forum C++ Programming
    Replies: 6
    Last Post: 09-29-2006, 11:19 PM
  4. insufficient memory for tsr
    By manmohan in forum C Programming
    Replies: 8
    Last Post: 01-02-2004, 09:48 AM
  5. Date program starts DOS's date
    By jrahhali in forum C++ Programming
    Replies: 1
    Last Post: 11-24-2003, 05:23 PM