Window

This is a discussion on Window within the C Programming forums, part of the General Programming Boards category; Hey, Im just beginning learning C. I got done compiling the exercise and ran it. When it was running, the ...

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    1

    Window

    Hey, Im just beginning learning C. I got done compiling the exercise and ran it. When it was running, the window would only stay up for half a second, then close. In that time, I could see parts of the things that I used the printf() function to print out, but it wont stay up long enough so I can see if I did the calculations correctly. Can anyone help me?

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    If you didn't take any input during the program then you can simply do this:
    Code:
    #include <stdio.h>
    
    int
    main(void)
    {
      /* Other stuff goes here */
      getchar();
    
      return 0;
    }
    That call to getchar will pause while waiting for input. Then you all you have to do to terminate the program is hit enter. If you did take input then it was probably with scanf and a single getchar probably won't work. In that case, the simplest method would be to do this:
    Code:
    #include <stdio.h>
    
    int
    main(void)
    {
      /* Other stuff goes here */
      while (getchar() != '\n') {
      }
      getchar();
    
      return 0;
    }
    The loop cleans up any mess left by scanf and the call to getchar after the loop has the same effect as the first program. Keep in mind that this is just putting a Band-Aid over the real problem and isn't recommended. But it should suffice until you learn the better ways of taking input.
    My best code is written with the delete key.

  3. #3
    Insomniac
    Join Date
    Mar 2004
    Posts
    35
    Assuming it's being ran in Windows, open up a shell, navigate to the directory you're saving the executeable in and run the program through that session.....

    just to save typing the user input code..
    i386 FreeBSD -- gcc-3.2.1 -- gdb-5.3

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Or you could always just read the FAQ.

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    Good ol' Quzah....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. WM_CAPTION causing CreateWindowEx() to fail.
    By Necrofear in forum Windows Programming
    Replies: 8
    Last Post: 04-06-2007, 09:23 AM
  2. 6 measly errors
    By beene in forum Game Programming
    Replies: 11
    Last Post: 11-14-2006, 11:06 AM
  3. Linking OpenGL in Dev-C++
    By linkofazeroth in forum Game Programming
    Replies: 4
    Last Post: 09-13-2005, 11:17 AM
  4. Pong is completed!!!
    By Shamino in forum Game Programming
    Replies: 11
    Last Post: 05-26-2005, 11:50 AM
  5. OpenGL Window
    By Morgul in forum Game Programming
    Replies: 1
    Last Post: 05-15-2005, 01:34 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21