Question:
Help with C++ compiler?
thief_of_allways
2009-09-11 01:41:07 UTC
I have a Bloodshed Dev C++ compiler. When I compile and run a program, it runs briefly (less than a second) then disappears. Can someone tell me how to fix this problem?
Four answers:
sandyqbg
2009-09-11 01:43:14 UTC
Under void main() before you close the last bracket '}', add this line:



getch();



This will make your compiler wait at your output screen
jplatt39
2009-09-11 02:19:34 UTC
Clifford may be sincere but changing compilers won't solve your problem. C and C++ were essentially designed for the command line, and the basic libraries intentionally do not include graphics or low-level screen control which would allow you to control how long something displays. When you use iostream or cstdio you are `creating a console application, which Windows will create a console for to run. As soon as the program completes its operation, Windows destroys the console. So it is not Dev C++ which is the problem, it is how you are using your programs.



You essentially have three choices. The first is to start a console (command.exe in run programs in XP -- I don't know what it is in Vista because I despise it so much) cd over to the directory where you have the executables, and run them from the command line. It won't close and you can inspect the output at your leisure.



The second choice is use a C standard function called system (in cstdlib) and an MS-DOS program called pause.exe. You put the line in "system("pause");" right before you return 0; and pause prints the message "Press Any Key" at the bottom of the console screen then waits for you to do so before going on to the next line which terminates it.



The final choice is to demand input at the end of the program, as pause.exe does. If you use conio.h you can just do a getch() and either throw the input away or create a character which accepts it as in c=getch();. If you use a curses you can do the same thing (but if you know curses you might consider switching over to FreeBSD or Linux for your programming). Any other function is likely to leave a newline in the input buffer which can complicate things for you.



These choices will work for any compiler on Windows, as any compiler will produce programs which do the same thing. So take your pick.
!
2009-09-11 01:46:13 UTC
Or you can try putting a char variable for the user to input at the end for the program to exit.



for example:



#include

using namespace std;



int main()

{



char quit;



cout << "Hello" << endl;

cin >> quit;



return 0;

}
Clifford
2009-09-11 01:50:13 UTC
i'll advice you to install the new edition of c++ that is visual basic2008!


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...