Skip to main content
GameDev.net gamedev.net
🔒 Locked

Having some noob problems!

Started by conf1de Sep 10, 2008 at 7:40 PM 12 replies 1.6k views
Original Post
conf1de
conf1de
Well, so I'm having the problem when i run the program below, the command window pops open and closed well fast, and wont stay open... Its the simple game over program as im just getting started ;] --------------------- // Game Over // My first C++ program #include int main() { std::cout << "Game Over!" << std::end1; } --------------------- I'm using Dev c++ Cheers, Kris.
Palidine
Palidine
it's a console project which means it's meant to be run from the command line: the black box, type the name of the exe. It closes because it exits right away.

For debugging purposes, put a breakpoint on the last curly brace when you run from the debugger. That way it'll stay open until you proceed past the breakpoint

-me
conf1de
conf1de
Haha yeah, just figured that out ;]
The L looks like a 1.

Fixed it, just changed it to:

// Game Over
// My First C++ program

#include
using std::cout;
using std::endl;
using std::cin;

int main()
{
cout << "Game Over!" << endl;
cout << "Press the enter key to exit";
cin.ignore(cin.rdbuf()->in_avail() +1);
return 0;
}

[Edited by - conf1de on September 11, 2008 7:20:01 AM]
Skaff
Skaff
You might also add a line like this at the end...

system( "pause" );

That should allow the program to wait for a keystroke at the end of the program. I am pretty sure you don't have to include any other headers for that to work. (possibly windows.h). But basically all that function does is put your passed string as an entered command line command. If you open up a console window and just type in "pause" it will just ask for you to hit a key. When working with some console based projects its nice to know. Using the "cls" to clear a window is also nice on some programs as well.
conf1de
conf1de
Ah yes, just replaced:

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

with:

system ( "Pause" );

A lot easier to remember! Cheers mate.
Not sure why i had the extra crap on the bottom before, edited that =/
rip-off
rip-off
Some things to be aware of if choosing to use std::system() with an external application to pause your program.
conf1de
conf1de
Ah ok, cheers man, read it there, so would you recommend using:

cout << "Press the enter key to exit";
cin.ignore(cin.rdbuf()->in_avail() +1);
ToohrVyk
ToohrVyk
It is recommended to use none of it. Having such a thing in a console application is annoying to the users (who may for instance want to use it in a batch): when the program is done, shut it down. Offer to terminate the program using the program's default input system if it's interactive.

Either way, your IDE should provide you with a way to keep your window around until you press a key (Visual Studio does, just press Ctrl+F5) and, failing that, a method such as using a breakpoint will also work unless your debugger is brain-dead.
conf1de
conf1de
Cant find how to keep it open in dev c++ without using them commands :(
Endless
Endless
You should try a different IDE. Dev c++ is simply, but very limited and eventually you will need to start using another IDE anyways. Personally I use Eclipse CDT. I've tried Code Blocks and it's pretty good too. And Microsoft Visual Studio Express seems to be the most popular free C++ IDE. In both Eclipse CDT and Code Blocks, the console window will stay open until you close it.
jpetrie
jpetrie
Dev-C++, for all its other failings, does have this option. I forget which one it is though, try various combinations of the "run" and "debug" options.

And upgrade to a decent IDE as soon as possible. Visual C++ Express is my recommendation.
Skaff
Skaff
rip-off-

Well... I am glad that I did make a comment on this thread to have a small flaw in some of my stuff pointed out. I really was not aware of some of the things going on under the hood. I liked the analogy of using a "bulldozer" to open the front door! While it might be perfectly find to put that option in a game for our players... I guess not quite the same should be done on our end :)
J0Be
J0Be
Quote:
Original post by conf1de
Ah ok, cheers man, read it there, so would you recommend using:

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


Try Ctrl F5, see if that doesn't leave it open.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.