Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

C question

  1. May 7, 2005 #1
    I wrote my code and try to setup the program , desperately the program closes it so quickly return 0 or getch() commands doesnt work how can I prevent it with a code ?
  2. jcsd
  3. May 8, 2005 #2
    ı mean the ms dos windows disappears so quickly..
  4. May 8, 2005 #3
    Yes if you execute the program from within windows the DOS window closes when the program ends. You could just open a DOS window and then execute the program from the command line, that way the DOS window will stay open.

    When characters are pressed they are placed in a buffer, and getch() will return the oldest character from the buffer, but if there is nothing in the buffer it will return ERR, I think (it depends on certain settings). But anyway you can use getchar(), that should work. It will wait until return is pressed.

    You could put something like this at the end of your program:
    Code (Text):
    printf("\npress q to quit\n");
      key = getchar();
    } while (key != 'q');
    Code (Text):
    printf("\npress q to quit\n");
      key = getch();
    } while (key != 'q');
  5. May 8, 2005 #4
    you can just put

    or scanf()
    to pause the prpogramme...
    else do a loop from 1...n and display something.
  6. May 27, 2005 #5
    in windows i use system("pause"); to prevent the window from disappearing. I dont really know if that is good way though.
  7. May 27, 2005 #6


    User Avatar
    Staff Emeritus

    Running your program from within the command prompt would also fix this problem.
  8. May 31, 2005 #7
    This problem arises in a few compilers. I guess you are not using Visual C++ cuz if you are using Visual C++, it automatically takes care of this issue.

    As others suggested you can simply use;

Share this great discussion with others via Reddit, Google+, Twitter, or Facebook