News:

Accounts with zero posts and zero activity during the last months will be deleted periodically to fight SPAM!

Main Menu

Result window does not stay

Started by kaiserniz, August 18, 2006, 08:48:24 AM

Previous topic - Next topic

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.

"We should forget about small efficiencies, say about 97% of the time: Premature quotation is the root of public humiliation."

kaiserniz


EricKane

other way,
You can insert code like below and include stdlib.h file:
...
system("pause");
return 0;

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

EricKane

QuoteI got told it's a bad habit to have, i never use it now. Using cin.get(); is a much better way.

sorry.I'm too stupid.
Ask a question: if i use C not C++,how can i do it?please teach me

takeshimiya

Quote from: Kane.zZ on August 18, 2006, 02:02:19 PM
QuoteI got told it's a bad habit to have, i never use it now. Using cin.get(); is a much better way.

sorry.I'm too stupid.
Ask a question: if i use C not C++,how can i do it?please teach me
The linked pointed says an example: getchar()


PDEE

#8
Kane.Zz,
Please don't pm me telling me what i should and should not do. I told kaiserniz what i have been told many times and that was to not use the sytem to pause with. I never said i was a teacher, it was just some friendly advice that most people like to have to make sure they are doing things the right way. ME included. You asked me how to do it yourself then pm me not to give you a lecture.  :shock: If you are going to pm me please have the decency to let me reply to you instead of 'please don't reply to this as i have not the time to read it' People give advice out all over the place. if you can't take the advice, don't ask for it!!

PDEE

EDIT: I apologize mandrav and moderators, but i had to say that. Won't happen again.