Preventing Program Closure with Code: Solutions & Tips

Click For Summary
SUMMARY

The discussion focuses on preventing a console application from closing immediately after execution in MS-DOS environments. Users recommend several methods, including using getchar(), getch(), and system("PAUSE") to keep the console window open. It is noted that executing the program from the command line instead of double-clicking it can also resolve the issue. Additionally, Visual C++ is mentioned as a compiler that automatically manages this behavior.

PREREQUISITES
  • Understanding of C/C++ programming language
  • Familiarity with console applications in MS-DOS
  • Knowledge of standard input functions like getchar() and getch()
  • Basic command line usage in Windows
NEXT STEPS
  • Research the differences between getchar() and getch() in C/C++
  • Learn about the system() function and its implications in C/C++
  • Explore command line execution of programs in Windows
  • Investigate how different compilers handle console application behavior
USEFUL FOR

Programmers developing console applications, students learning C/C++, and anyone troubleshooting console behavior in Windows environments.

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
3K
  • · Replies 26 ·
Replies
26
Views
4K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 9 ·
Replies
9
Views
3K
Replies
38
Views
3K
Replies
6
Views
9K
Replies
16
Views
3K
  • · Replies 10 ·
Replies
10
Views
861
Replies
3
Views
2K