Now that I've figured out how to maintain an open DOS window with cin.get, whenever I run my program and press ENTER, the window automatically closes? Does anyone know why this is and how to fix it? T|-|4|\||< j00!
L33T R|_|L35!
Now that I've figured out how to maintain an open DOS window with cin.get, whenever I run my program and press ENTER, the window automatically closes? Does anyone know why this is and how to fix it? T|-|4|\||< j00!
L33T R|_|L35!
*stabs fork through eye*Now that I've figured out how to maintain an open DOS window with cin.get
-edit-
But honestly speaking....The reason it closes is because your program finishes running. Without a cin.get() (or getch() ) at the end, it finishes running and closes.
-edit 2-
example:
When you run this program, this is what happens:Code:#include <iostream.h> int main() { cout << "Blah"; // You won't see this text because // the dos console will go away right away return 0; }
*DOS console pops up*
*DOS console pops away, right away, before your eyes have a chance to even process what was written*Code:|------------------------------------------| |DOS Console, wheeee! | |------------------------------------------| | | | Blah | | | |------------------------------------------|
However, if you put a cin.get() in there or some other method of pausing the program, it will wait until
a) the user pressed enter (with cin.get())
b) any key is pressed (with getch())
c) the program deems it's finished (with a while loop)
So add an extra step in there right before "DOS console pops away" like so: "DOS console stays active, waiting for input or doing some other sort of processing"
Last edited by jverkoey; 07-02-2004 at 10:09 PM.
man im impressed you took the time to draw that dos box !
I would like to offer
as a method to halt your program until a keypress.Code:system("pause");
- "Problem Solving C++, The Object of Programming" -Walter Savitch
- "Data Structures and Other Objects using C++" -Walter Savitch
- "Assembly Language for Intel-Based Computers" -Kip Irvine
- "Programming Windows, 5th edition" -Charles Petzold
- "Visual C++ MFC Programming by Example" -John E. Swanke
- "Network Programming Windows" -Jones/Ohlund
- "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
- "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel
thats overkill for a simple app. heres (a better explanation than i can give) why.Originally Posted by The Brain
i've seen several people use system("PAUSE") when they want to delay their programs. i'm not sure who's teaching this method, but it's a bad habit.
by calling system(), you are invoking the default shell. the shell then executes the commandline given to it ("PAUSE", in this case). that is, it runs the 'pause.exe' program. so, now your simple c program is relying on two external programs for a stupid thing like pressing a single key. what if someone deleted or renamed 'pause.exe'? what if someone tried to compile your program on unix, or a mac? it wouldn't work. you'd get an annoying shell message and no pause. besides, why have the overhead of launching 2 programs, when you can accomplish the same thing in c?
FAQ!
And say it with me now: "Leet speak isn't cool"
Wh47, 7h4n705?
C++:
Code:#include <iostream> void wait() { std::cin.clear(); // std::cin.ignore( std::cin.rdbuf()->in_avail() ); std::streambuf * pbuf = std::cin.rdbuf(); std::streamsize size = pbuf->in_avail(); std::cin.ignore(size); std::cin.get(); } int main() { //... wait(); }