1. This forum is in read-only mode.

How to keep window open in c?

Discussion in 'Computers & Modding' started by vivo, May 18, 2011.

  1. vivo

    vivo Well-Known Member

    Hello
    I have tried to use system pause in my c program but it will only display press any key to continue. Any other ideas? (Dev cpp)
     
  2. Loonylion

    Loonylion Administrator Staff Member

    for what reason do you want to keep the window open?
     
  3. vivo

    vivo Well-Known Member

    (The command prompt) because it opens the closes with regular code. And when I use cmd prompt it does not display anything (even though a printf is first)
     
  4. Loonylion

    Loonylion Administrator Staff Member

    if its not displaying anything when you run it via command prompt then theres something wrong with your code. What you probably should do is when it finishes, output a message along the lines of 'Press Q to quit' and then loop waiting for the user to press q.
     
  5. vivo

    vivo Well-Known Member

    Like let's take the hello world example. If you just put that in, should it work? My code compilesfine without errors so...dk
     
  6. Loonylion

    Loonylion Administrator Staff Member

    It's a while since I've used C/C++, but

    Code:
    int main(int argc, char* argv[])
    {
    	printf("Hello World!\n");
    	return 0;
    }
    
    will output 'Hello World' and terminate. A console program will always terminate on completion unless programmed otherwise.
     
  7. vivo

    vivo Well-Known Member

    So haw do O keep it open?
     
  8. markswan

    markswan Well-Known Member

    Put some "getchar"s in there, otherwise the program will end without waiting for user input.

    #include <stdio.h>
    int main()
    {

    printf("Hello vivo");
    getchar();

    }
     
  9. vivo

    vivo Well-Known Member

    Ok thank you. I appreciate all the help :)