See the output on the screen after debugging

Hi everyone,

I am very new to C++, I am working on a cryptography project. After building the solution on visual studio, when i debug it, a black window (like command prompt) comes for few seconds and disappears.
As I interpreted that is the place where I have to input a number there, using srand a random number is generator.

I want to increase the time of that window so that I can enter the input and see the random generator generated. This question might be silly but I want to know why that screen disappears so quickly.
/* srand example */
#include <stdio.h> /* printf, NULL */
#include <stdlib.h> /* srand, rand */
#include <time.h> /* time */

int main ()
{
printf ("First number: %d\n", rand()%100);
srand (time(NULL));
printf ("Random number: %d\n", rand()%100);
srand (1);
printf ("Again the first number: %d\n", rand()%100);

return 0;
}

Waiting for any help......
Thank you
This actually is very common. Because Visual Studio doesn't compile in a Pause function, you will have to break your code manually:

1
2
3
4
5
6
7
8
9
.
.
.
printf ("Again the first number: %d\n", rand()%100);

system("PAUSE");

return 0;
}


The "system" function passes the argument "PAUSE" to the command prompt.
This will make your code stop execution before the return statement, and as such the prompt will stay open :)
This solved my problem, thank you so much :)
I want to know why that screen disappears so quickly.

The reason is because console apps are intended to run interactively in a Command Prompt. While adding code to pause your app when running from Visual Studio might help when testing, it will be a bit irritating when run from the command line.

Andy

PS you might want to check out the pinned thread at the top of the Beginner's forum, on this very topic: Console Closing Down
http://www.cplusplus.com/forum/beginner/1988/
Inc. why system() is evil !!
As andy just stated,

never use system(); in an actual application, if that application is supposed to be run from the command prompt! Just use it, very carefully, when debugging your program from within VS
Setting the subsystem of your project to "console" is the correct way to address this issue in VS.

In VS, visit your projects settings (alt+f7 is the default shortcut).
Then, you'll find the setting you need under:
Configuration Properties -> Linker -> System -> SubSystem
You should change that setting to: Console (/SUBSYSTEM:CONSOLE) and hit the "Apply" button. Failure to hit the "Apply" button will result in the change not taking place. From then whenever you start your program from within the IDE via Start Without Debugging (default shortcut - ctrl+f5) the program should pause when it is done.
Last edited on
Topic archived. No new replies allowed.