Hello, I'm new to using C++ and am using dev-c++ as a compiler only
I'm having a problem as the code I've written doesn't stay open after
the user has stated the name, here's the code.

//Game 3,0
//Using constants and strings.

#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;
    //getting 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 desired name: ";
    cin >> leader;
    //telling the story
    cout << "In calradia there was a band of men " << adventurers << " in size.\n";
    cout << "They set out to become a strong army, unfortunately " << killed << " of them died.\n";
    cout << "The band was ruled by " << leader << " and he did not expect to be attacked by\n";
    cout << "-another band of men. The opposing band lost but this is where " << leader << " lost\n";
    cout << killed << "of his men. However he still had " << survivors << " left.\n";
    cout << leader << " thought it a good idea to pay the band for their work, each\n";
    cout << "recieved " << GOLD_PIECES / survivors << "." << leader << " kept the rest.\n";
    cout << "This amounted to " << GOLD_PIECES % survivors << " and everyone was happy.\n";
    cout << "Unfortunately the " << killed << " were forgotten and their corpses left to rot.";
    cin.ignore(cin.rdbuf()->in_avail() + 1); /* This should stop the
    console closing right? */
    return 0;

I'll have you figure this is self explanetory as I'm working out of a
book, I've added a comment to the line I believe halts the closing
but it still doesn't output the story before it closes. Any help?

Replace your cin.ignore(cin.rdbuf()->in_avail() + 1); call with cin.ignore(); cin.get(); the get() will wait for a keypress from the user.

Edited 6 Years Ago by jonsca: n/a

Thanks it worked I'll keep notes like this in future as it looks like it may help.

Possibly, but in this case it did not. There was some excess in the input buffer that was causing the program to skip right past the get().

This article has been dead for over six months. Start a new discussion instead.