Question:

Simple C programming question?

by  |  earlier

0 LIKES UnLike

Hi, I'm new at programming so please bear with me. I'm using Dev C++ to write a simple C program on Windows XP. I can get the program to compile and save correctly to a .exe file, but when I run that file it will not stay on my screen. So if I do a simple printf, a black screen pops up incredibly quick with the output and then is gone. How do I make it stay up? Thanks

 Tags:

   Report

3 ANSWERS


  1. The normal with C++ is to have it wait for another input.  Since you are using C, this may be a bit more tricky.  A quick hack would be to stick it into an infinite loop till you press a certain key, but you would have to be able to listen to keep strokes.  If you aren't able to do that, then you best solution would be to add at the end of the program this line of code:

    while(true);

    You would have to manually end the program though, so you should stop using this as soon as you learn a different method.


  2. dam..rough crowd.

    use the getchar( ); function. place it at the end of your main before the return.

    #include <iostream>

    int main()

    {

    // your programming

    getchar( );

    return 0;

    }

  3. First of all, don't listen to the first poster (3+ years my ***).

    With that said, the C++ program that you wrote is executed in a console. So when you start the .exe program, your Operating System starts the console -> starts your program -> as soon as your program is done the console is closed. That is why you don't have a chance to view your results. There are some ways to solve this, like waiting for user to press a button before exiting. However don't do this.

    There's only one real solution to the problem. STOP USING IDE's!!! Delete Dev C++. With stupid programs like these you do not fully understand what's going on! Do you really know what compilation is? What is linking? What is a translation unit? What is an object file? IDE's like Dev C++ hide all this information from you. Get yourself a compiler (I suggest gcc). Press start button -> run -> cmd and do all your work from the console.

Question Stats

Latest activity: earlier.
This question has 3 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.