Hey! I GUARANTEE that I'm the most junior programmer here! I started programming VB about 2 months ago, and I started Dev-C++ yesturday!!! I actually have a quick question regarding Dev-C++, I would be very grateful for an answer on this ^_^

I have just purchaced the "Beginning C++ Game Programming" by Michael Dawson... and I'm very confused about the following function:

std::cout << "Press the enter key to exit";
std::cin.ignore(std::cin.rdbuf()->in_avail() + 1);

I am aware that by "int"ing the cout and cin things, I no longer have to type in the std bit:

cout << "Press the enter key to exit";
cin.ignore(cin.rdbuf()->in_avail() + 1);

But it just WONT work for some reason! As soon as the program has compiled, the screen flashes on and flashes off! Do you know how to make this function available to all of my created programs, so that I am able to manually close down my created program without the fustration of automatic return 0;!

For example, (I don't know if anyone is familiar with this source code, but I'll give it a mention anyway):

The "Lost_Fortune" program asks the user to input a number, then a smaller number and then your surname... I enter all of these things and then, (when it is supposed to show me my results) it flashes on and off so fast that I can't read it! I'm sure that you have had millions of junior programmers such as myself asking the same old question, but I would be grateful for a reply! Thanks all!

Thank you VERY much!!! All the best.

You need the std:: in front of cout and cin. You are using the standard namespace. You use it when you are using the <iostream> header file because it is wrapped in the standard namespace. (I could be wrong. I am going to check it out and correct it if I have too). Can you post the whole code, this might be helpful. Put the code in code tags.

The program that you had trouble seeing is in dos I'm assuming. To fix this open up the dos command prompt. Then you have to find the directory you put it in and tell dos to open it, or you can simply drag the program into the dos window and press enter. This makes the window stay open so you can see the program.

It also might be good to get familar with c++ before you start trying to game program. It will help you out in the long run, and will make everything clearer.

Ok, thanks DarkOmen!

I'll give it a try! I'll also paste this code in for you!

Thanks again

Ok, I didn't know how do put this snippet into the C++ section, so here it is...
Sorry if you wanted to read it in the code section!

#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 from the user:
	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 rogue, " << 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 adventurers, " << 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";

	return 0;
}

Now obviously I would put in the:

std::cout << "Press the enter key to exit";
std::cin.ignore(std::cin.rdbuf()->in_avail() + 1);

bit right before the return 0; but even when I try that it won't stay open for more than a split second... I hope this is going to help you understand my problem. You'll probably find that DOS opens and closes REALLY fast for you too, but i'm sure you'll be able to figure something out!

Thanks DarkOmen!

its crude but if you add the following near the top (before main)

#include <cstdio>
using namespace std; // you dont need std:: if you use the std names (this means you could get rid of the "using... " statements...

then use the function getchar like so:

cout << "Press the enter key to exit";
getchar(); // replaces the cin.ignore stuff
return 0;

IE:

#include <iostream>
#include <string>
#include <cstdio>

using namespace std;

int main()
{
	const int GOLD_PIECES = 900;
	int adventurers, killed, survivors;
	string leader;

	//get the information from the user:
	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 rogue, " << 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 adventurers, " << 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 << "Press enter to exit";
             getchar();
	return 0;
}

Ok, I didn't know how do put this snippet into the C++ section, so here it is...
Sorry if you wanted to read it in the code section!

The code snippet library is a resource of fully working code snippets and functions. So you did right posting it here :)

Thank you both!

I'll soon be adding my own codes to your site!

Take care

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.