Sign in to follow this  
JayDawg

DOS window problem

Recommended Posts

Hi all, First time posting, and I know this is a really dumb problem, but I just started learning C++ with the Beginning C++ Game Programming book by Michael Dawson. Anyways, my problem is getting the DOS window that pops up to stay on screen (the one that comes up when you run your little program after compiling). The book provides a bit of code you can use so you have to press enter to exit, but that only works some of the time, and I know there is a setting in windows or something that allows the DOS window to remain open until you terminate it, I just cannot find it. I am on WinXP Pro SP2, using Bloodshed DevC++, thanks a load for the help!!!

Share this post


Link to post
Share on other sites
Thanks for the help. The command prompt method works just fine, but putting #include <stdlib.h> with the other #includes at the top, and adding getch(); generates an error on the getch(); line. Here is a copy of the code, it's one of the examples in the book (and I added what you mentioned).

#include <iostream>
#include <string>
#include <stdlib.h>

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 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";

getch();
return 0;
}

Thanks so much again!

Share this post


Link to post
Share on other sites
Gave it a go... gives the exact same error on the line with getch(); So I have no darn clue what to do :| Sucks to try and learn something but you can't see the results really you know? Even if it's dinky stuff like this, still is nice to be able to view it!! Thanks for the try, and if anyone else knows anything, please help!

Share this post


Link to post
Share on other sites
Hmmm... How am I to apply use of cin.get();? It doesn't give me an error or anything if i add it just before return 0; (remember, i'm a brand new beginnner here, so if there's anything assumed by you guys, I probably wont know it), but however it does not fix the issue, the window still closes. I am using Bloodshed Dev C++ 4.9.8.0 to compile and run. Thanks alot for the speedy replies and your time.

UPDATE:
I tried the #include <conio.h> along with getch() and it worked perfectly! Thanks a bunch, I am releived to finally get this issue fixed. I would however still like to know about the cin.get(); and how I might get that one to work, if anyone has a chance.

Share this post


Link to post
Share on other sites
Quote:
Original post by fuchu

//your code goes below
...
system("PAUSE");
...


Sourt of standard, but easily hackable, especially on linux, and not all os's have the same name for their pause function.

Share this post


Link to post
Share on other sites
Operating Systems (mac, windows, linux)

If you're staying on windows, then use either, but if you want to move to linux, i would highly reccomend you steer clear of system, as it can be exploited.

Share this post


Link to post
Share on other sites
Quote:
Original post by fuchu
I will start using my_pause(); because I want to make a game compatible for Windows, MasOS, and Linux.


Remember, however, that it's not sure it will always work. If you have pressed enter earlier in the program. it may still be in the keyboard buffer. If that's the case, cin.get() will read from there, and if it finds an enter press, it will take that instead of waiting for input.

There is a few ways to solve this problem. I will list some of them below.

void my_pause()
{
cout<<"Press [ENTER] to continue."<<endl;
cin.get(); //This extra cin.get() will take the input from the buffer (if there is any)
cin.get(); //...and this will wait for new input.
}

/**************************************************/

void my_pause()
{
cout<<"Press [ENTER] to continue."<<endl;
fflush(STDIN); //Clears stdin, which in this case is the keyboard.
cin.get();
}

/**************************************************/

void my_pause()
{
cout<<"Press [ENTER] to continue."<<endl;
cin.ignore(255, '\n'); //Reads and discard all characters until it finds a newline.
cin.get();
}




I cannot guarantee that any of this will work, but I think it will.

Share this post


Link to post
Share on other sites
Oh I see, very cool. Though the inclusion of <conio.h> and using getch(); fixed my problem, I can see now how cin.get() functions, which is just what I was looking to find out! Good to learn this stuff, thanks to everyone for posting, these forums are incredibly helpful.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this