Sunday, August 2, 2009

How can I prevent a program that is written in C from closing immediately?

I write a source code in C for a simple program. Then I compile it. When I execute the program, it closes down immediately. What can I add to the code to prevent it from closing? I want the program to wait for my approval before closing.

How can I prevent a program that is written in C from closing immediately?
1)


#include%26lt;conio.h%26gt;


#include%26lt;....%26gt;


#...


/*





YOUR CODE HERE





*/


getch();
Reply:just read some string at the end of the c program. that will do it. and don`t forget to print out the "press enter to exit" message,
Reply:1) Put a simple input command before exiting.


int x;


cin %26gt;%26gt; x;


return 0;





2) Or if you are running it on windows, add these 2 lines to your program.


#include %26lt;cstdlib%26gt;





int main(){





system("PAUSE");


return 0;


}
Reply:Hi, dude just write a batch file( open notepad ) something that looks like this:





nameOfYourApp.exe


pause





and then save it as whatever.bat


then double click in it and that should work.





Hope this help
Reply:or you could put a wait() statement before the last statement of your program and your program will wait for a signal to end. The other option is a sleep() statement. If you use the sleep() statement, you can give it an amount of time in seconds you want your program to hang around before quiting as in:





sleep(5); /* sleep 5 seconds */

community survey

1 comment: