User forums > Using Code::Blocks
Result window does not stay
kaiserniz:
Hi all, I'm completely new to programming, and am facing this problem on day 1. I typed in this code from a book:
#include <stdio.h>
#include <math.h>
int main(void)
{
double x1=1, y1=5, x2=4, y2=7,
side_1, side_2, distance;
side_1=x2-x1;
side_2=y2-y1;
distance=sqrt(side_1*side_1 + side_2*side_2);
printf("The distance between the two points is "
"%5.2f \n",distance);
return 0;
}
I saved the file and ran the exe, and the result window just kinda blinked away. How can I make it stay and display the result until I close the window?
Thanks in advance.
thomas:
If you mean this happens when you run the program from inside Code::Blocks, you need to enable the checkbox "Pause when execution ends" inside your project's properties. That will keep the console open until you hit a key.
If you mean that this generally happens when you run the program by double-clicking, then:
1. Please do note that we don't run a general programming forum here. This forum is about issues related to Code::Blocks. Please consult a general programming forum for questions like this.
2. It is perfectly normal behaviour. The program runs and exits after it is done, that's just what you programmed. If you want the program to pause when it is done, you must program this (for example by waiting for a key).
The "output window" is nothing but a DOS shell that's implicitely created. When it is no longer needed, it is closed.
A better approach than blocking the program is to open a DOS window first and run the program from there (by typing its name). The window will stay open after your program finishes, and your program does not block needlessly.
Under an operating system other than Windows (for example Linux), that's the only possibility anyway, since running a console program does not automatically open a shell window.
kaiserniz:
Thanks for answering. I got it.
EricKane:
other way,
You can insert code like below and include stdlib.h file:
--- Code: ---...
system("pause");
return 0;
--- End code ---
For more info,please see about C standard library,function int system(const char *command);
By the way,I'm using windows system console(cmd),not cb_console_runner.
with regards
kane
PDEE:
Re- System("pause");-- I got told it's a bad habit to have, i never use it now. Using cin.get(); is a much better way. Read this: http://www.gidnetwork.com/b-61.html
PDEE
Navigation
[0] Message Index
[#] Next page
Go to full version