System Pause

Hey, whenever I write a C++ program it flashes the window for 1 second then it goes away. When i add SYSTEM("PAUSE") at the end it starts to work but I don't want to have to do that every time and in and out of my source code. Is there any other solution?
Did you used the visual studio ?
Press F5 will show that problem!
You can press Ctrl + F5.
Thanks, that works great!
Write a simple method to a class named Environment, let's say, "enter_to_go();" displaing the message - "hit enter to go ahead." and every time you need a pause, write in the code "enter_to_go();" or something shorter.

I have it here the class is Ambiente.h (sorry, mainly in Portuguese) and the method
is apeteco2() - which means "hit enter to go".

http://www.calculo-numerico.sobralmatematica.org/programas/

Look directly apeteco2() in Ambiente.h - is all you need.

Tarcisio

Hey, I've tried all of this but is there a way to make the end result (.exe) to run without the debuging so it will pause without extra code?
Certainly.

Visual Studio has two types of builds: Debug and Release.

The default build is Debug and that means code is inserted in the .exe to support the debugging files. The Release build has not debugger support.

When you execute a program using Start (F5), Visual Studio sees that it is a debug .exe and assumes you have the necessary pauses already inserted. If you don't, you see a black flash which is the console screen going by as your program executes.

You may also execute your program using Start Without Debugging (CTRL+F5). WWhen you do this, you tell Visual Studio that while this is a debuge .exe, you are not using your debugger. In this case, Visual Studio will pause at the end of main() with:

Press any key to continue...

Therefore, you never need any special code in your program for testing.
Hey, CTRL+F5 seems to work but where does it save the exe that is NOT injected. When I locate the exe in My Doctuments >> Visual Studio 2005 >> Projects >> Project 1 >> Debug >> Project1.exe it flashes in and out.
That's because you are executing the program outside of Visual Studio.

That black console window you see in Visual Studio is not the command interpreter. It is a black window generated by Visual Studio to make you think it's the command interpreter- but it ain't. It's a special window controlled by Visual Studio so the F5 vs. CTRL+F5 works.

In fact your .exe is a debug .exe without pauses. When you execute directly using the command interpreteer, you get no pauses.
Oh OK, so if I want to run the exe out of visual studio I have to put the pause in?
Yes, indeed.

Outside of Visual Studio, your program must contain all of the instructions it will ever execute.
OK, wel I heard somewhere that when you program for windows then it will have pauses automatically just because of the window and everything. Is that true? And also, if I am to put pauses in how would I pause in the middle of a program?
A Windows program is not a ANS/ISO C++ program. It does not use main(). Your Windows programs runs under the auspices of the operating system plsu whatever API your are using. Pauses couild very well be automatically inserted.

What kind of pause do you need?
1) The user needs to respond: use a cin>> and check for Y or N
2) The user does not need to respond AND your are using Windows, try Sleep(seconds);

Otherwise, you can create a Windows Console Application and use message boxes.
yea, thats what I mean. When doing an empty project... just a command application you have to have pauses even with user input like CIN >>
Yes. There is no support for pauses (and many other things- like multithreading) in C++. Instead, you write C++ functions to implement these features.

Operating systems have a slew of services like this that you can call so you don't have to write from scratch.

C++ is a programmer's language and not an application language like Visual Basic or C#. You would write those languages in C++.
1
2
3
cout << "Press RETURN to continue...";
cin.get();
return 0;

or write a time delay function.

EDIT: I have posted a delay and clear function in the Lounge. I invite you to use it.
Last edited on

cout << "Press RETURN to continue...";
cin.get();
return 0;

or write a time delay function.


Try it !
try using a flush and hold method such as:
fflush(stdin);
getchar();
return 0;

That'll hold your program open until a key is pressed.

-Ryan
You can repeat the program until the user prompts to close it. Say for a game.

1
2
3
4
5
6
7
8
9
10
11
int main()
{
       char input;
       do{
             DoGameStuff();
             cout << "Play Again?" << endl;
             cin >> input;
        }while(input == 'y' || input == 'Y')
        return 0;
}
              



So no need for a pause, but it will stay open if the user says so, otherwise it will end. Something to keep in mind.
Last edited on
Topic archived. No new replies allowed.