Thread: C++ Help With Prog Closing

  1. #1
    Registered User
    Join Date
    Dec 2005

    C++ Help With Prog Closing

    I just started C++ coding about 2 days ago and I'm using a book. Its a great book and I have made scripts with it but with 1 major problem. Whenever I run my prog it instantly closes. The book said if I encountered this problem I should use
    cout << "Press the enter key to exit the program!" << endl;
    But this won't work in situations where I have to press the enter key to accept something in a script. I am currently making this script
    #include <iostream>
    #include <string>
    using std::cout;
    using std::cin;
    using std::endl;
    using std::string;
    int main()
        const int GOLD_PIECES = 900;
        int adventurers, killed, survivors;
        string leader;
    // Get the information
        cout << "Welcome to Lost Fortune\n\n";
        cout << "Please enter the following for your personalized adventure\n";
        cout << "Enter a number: ";
        cin >> adventurers;
        cout << "Enter a number, smaller than the first: ";
        cin >> killed;
        survivors = adventurers - killed;
        cout << "Enter your last name: ";
        cin >> leader; 
        // Tell the story
        cout << "\nA brave group of " << adventurers << " set out on a quest ";
        cout << "-- in search of the lost treasure of the Ancient Dwarves. ";
        cout << "The group was led by that legendary rouge, " << leader << ".\n";
        cout << "\nAlong the way, a band of marauding ogres ambushed the party! ";
        cout << "All fought bravely under the command of " << leader;
        cout << ", and the ogres were defeated, but at a cost. ";
        cout << "of the adventurerers. " << killed << " were vanquished, ";
        cout << "leaving just " << survivors << " in the group.\n";
        cout << "\nThe party was about to give up all hope. ";
        cout << "But while laying the deceased to rest, ";
        cout << "they stumbled upon the buried fortune. ";
        cout << "So the adventurers split " << GOLD_PIECES << " gold pieces.";
        cout << leader << " held on to the extra " << (GOLD_PIECES % survivors);
        cout << " pieces to keep things fair of course.\n";
        cout << "\n\n\n\nPress the enter key to exit the program!" << endl;
        return 0;
    What should I do to keep the program from exiting??


  2. #2
    the Great ElastoManiac's Avatar
    Join Date
    Nov 2005
    Republika Srpska - Balkan
    add this above the "return 0;"
    int nothing;
    lu lu lu I've got some apples lu lu lu You've got some too lu lu lu Let's make some applesauce Take off our clothes and lu lu lu

  3. #3
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Use this before return 0.

    ignore() extracts the special enter character from the stream, and get() requests a character. This would not work if more than one character was in the stream (somewhat rare in tests, or little projects). You could use cin.ignore(256,' '); to be almost sure, or use <climits> to be accurate, in that case.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  4. #4
    Registered User
    Join Date
    Dec 2005

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Interpreter.c
    By moussa in forum C Programming
    Replies: 4
    Last Post: 05-28-2008, 05:59 PM
  2. YangHui triangle print test prog?
    By toysoldier in forum C++ Programming
    Replies: 8
    Last Post: 08-20-2004, 08:46 AM
  3. Idea: Elevator Prog
    By gamer4life687 in forum Contests Board
    Replies: 0
    Last Post: 11-15-2002, 10:01 PM
  4. Try my prog...
    By Commander in forum A Brief History of
    Replies: 0
    Last Post: 05-09-2002, 07:43 AM
  5. password prog
    By ihsir in forum A Brief History of
    Replies: 10
    Last Post: 01-06-2002, 06:39 AM