Preventing Program Closure with Code: Solutions & Tips

Click For Summary

Discussion Overview

The discussion revolves around methods to prevent a console program from closing immediately after execution in a Windows environment. Participants explore various coding techniques and command-line approaches to keep the console window open for user interaction.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Homework-related

Main Points Raised

  • One participant expresses frustration with their program closing immediately and seeks a coding solution to prevent this behavior.
  • Another participant suggests executing the program from a command line to keep the DOS window open after execution.
  • Several participants propose using commands like getch(), getchar(), or system("pause") to pause the program and wait for user input before closing.
  • There is mention of different behaviors in various compilers, with one participant noting that Visual C++ handles this issue automatically.

Areas of Agreement / Disagreement

Participants generally agree on the need for a solution to keep the console window open, but there are multiple proposed methods and some uncertainty about the effectiveness of each approach.

Contextual Notes

Some methods may depend on specific compiler settings or environments, and there is no consensus on the best approach among the participants.

Who May Find This Useful

Programmers working in C/C++ on Windows who encounter issues with console applications closing immediately after execution.

LasTSurvivoR
Messages
16
Reaction score
0
I wrote my code and try to setup the program , desperately the program closes it so quickly return 0 or getch() commands doesn't work how can I prevent it with a code ?
 
Computer science news on Phys.org
ı mean the ms dos windows disappears so quickly..
 
Yes if you execute the program from within windows the DOS window closes when the program ends. You could just open a DOS window and then execute the program from the command line, that way the DOS window will stay open.

When characters are pressed they are placed in a buffer, and getch() will return the oldest character from the buffer, but if there is nothing in the buffer it will return ERR, I think (it depends on certain settings). But anyway you can use getchar(), that should work. It will wait until return is pressed.

You could put something like this at the end of your program:
Code:
printf("\npress q to quit\n");
do
{
  key = getchar();
} while (key != 'q');
or
Code:
printf("\npress q to quit\n");
do
{
  key = getch();
} while (key != 'q');
 
you can just put

getch()
or scanf()
to pause the prpogramme...
else do a loop from 1...n and display something.
 
in windows i use system("pause"); to prevent the window from disappearing. I don't really know if that is good way though.
 
Running your program from within the command prompt would also fix this problem.
 
This problem arises in a few compilers. I guess you are not using Visual C++ because if you are using Visual C++, it automatically takes care of this issue.

As others suggested you can simply use;

getchar();
getch();
system("PAUSE");
 

Similar threads

  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 26 ·
Replies
26
Views
4K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 9 ·
Replies
9
Views
3K
Replies
38
Views
5K
Replies
6
Views
10K
Replies
16
Views
4K
  • · Replies 10 ·
Replies
10
Views
2K
Replies
3
Views
2K