Thread: Help with basic C program closing

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    26

    Help with basic C program closing

    Hi,
    Im learning C and currently going through the tutorials on Cprogramming.com. However when I compile there second 'example' I enter a number and it closes too quick to read the output. If I run it in debug mode on Bloodshed Dev c++ it stays open and I can read the output. Ive tried adding things like return 0; to the end but im new to this and it never made a difference can anyone help me get the window to stay open?

    Code:
    #include <stdio.h> 
    int main() 
    {   
    int this_is_a_number;   
    printf( "Please enter a number: " );   
    scanf( "&#37;d", &this_is_a_number );   
    printf( "You entered %d", this_is_a_number );   
    getchar();
    }

    Thanks in advance

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Add another getchar() - the reason your program is closing is that the newline from entering the number is still in your input buffer when you reach the getchar() at the end of your code, and that means that it's immediately "satisfied", and not waiting for your to hit any keys.


    You should probably also return 0 at the end of your main function.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strcar keep closing my program
    By inmaterichard in forum C Programming
    Replies: 4
    Last Post: 07-26-2007, 01:15 PM
  2. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  3. Problem with basic encryption program.
    By mmongoose in forum C++ Programming
    Replies: 5
    Last Post: 08-27-2005, 04:41 AM
  4. IDEA: A basic drawing program
    By ygfperson in forum Contests Board
    Replies: 0
    Last Post: 08-12-2002, 11:15 PM
  5. Replies: 2
    Last Post: 05-10-2002, 04:16 PM