# DOS window problem

## Recommended Posts

JayDawg    127
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 on other sites
Mulligan    378
2 ways, one, run your program from the command prompt

two, add getch(); just before return 0; at the end of main().
#include <stdlib.h>

should do it

##### Share on other sites
JayDawg    127
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;

//get the information
cout << "Welcome to Lost Fortune\n\n";

cout << "Enter a number: ";

cout << "Enter a number, smaller than the first: ";
cin >> killed;

cout << "Enter your last name: ";

//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 on other sites
Clueless    185
Try #include <cstdlib> instead of <stdlib.h>
I think stdlib.h is deprecated!? Not sure if that helps, though.

##### Share on other sites
JayDawg    127
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 on other sites
Lajnold    226
getch() is in conio.h. But it isn't part of the standard, so it's not sure you can use it; it depends on what compiler you are using.

##### Share on other sites
Deranged    668
use cin.get(); it is standard, as it is part of std::cin

##### Share on other sites
JayDawg    127
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 on other sites
Deranged    668
void my_pause(){    cout<<"Press [ENTER] to continue."<<endl;    cin.get();}int main(){    my_pause();    return 0;}

Standard pause.

##### Share on other sites
fuchu    100
//your code goes below...system("PAUSE");...

##### Share on other sites
Deranged    668
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 on other sites
fuchu    100
Then is it better to use my_pause();?
Did you mean compilers on "os's"?

##### Share on other sites
Deranged    668
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 on other sites
fuchu    100
I will start using my_pause(); because I want to make a game compatible for Windows, MasOS, and Linux.

##### Share on other sites
Lajnold    226
Quote:
 Original post by fuchuI 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 on other sites
JayDawg    127
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.