Thread: program not staing open

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    13

    Question program not staing open

    im very new to c++ so i might be missing something but when i try to run a very simple hello world program...
    Code:
    #include <stdio.h>
    int main()
    {
        printf("Hello World\n");
        return 0;
    }
    it opens and closes immediately. i asked my friend and he told me that i need to run a while loop, so i made it do a ridiculous never ending ask to keep it open but it still closes
    Code:
    #include <stdio.h>
    int main()
    {
        printf("Hello World\n");
        return 0;
        int j = 1;
        while (1 == 1);
        j = j + 1;
    }
    can anyone help me?

  2. #2

  3. #3
    Registered User
    Join Date
    Dec 2004
    Posts
    465
    Code:
    #include <iostream>
    #include <stdio.h>
    int main()
    {
        printf("Hello World\n");
    
        cin.get();
        return 0;
    }
    That should do the trick. Wait why do you have return 0 in the middle of your program?
    My computer is awesome.

  4. #4
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    Also, that is C code not C++.

    It is usually not a good idea to mix the two languages unless you are using a C library that C++ does not have ( which I think is none ).

    Cerin is correct. You call return 0 imidiatley after hello world prints so as soon as your computer has displayed the message it returns the value to the OS and closes the program.
    Return 0 should be the last line of the main function before the closing brace ( unless you are using return EXIT_SUCSESS
    Double Helix STL

  5. #5
    Registered Usurer
    Join Date
    Apr 2005
    Location
    upstate NY
    Posts
    79
    Sorry, can't help but mention the other obvious problem: [URL="http://dictionary.reference.com/browse/staing"]
    Last edited by -JM; 04-08-2007 at 01:49 PM.
    Huh?

  6. #6
    Registered User
    Join Date
    Mar 2007
    Posts
    7
    you can also use
    Code:
    system("pause")

  7. #7
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Quote Originally Posted by bjl19901 View Post
    you can also use
    Code:
    system("pause")
    Only on Windows I believe. As a general rule, I think implementing functionality based upon system() is a bad idea. There are exceptions to this, and some would say system("pause") is one, but I still dislike it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. program to open IE
    By x2012 in forum C# Programming
    Replies: 2
    Last Post: 11-03-2006, 09:07 PM
  2. File Types Automatically Open With My Program
    By ElWhapo in forum Windows Programming
    Replies: 3
    Last Post: 12-29-2004, 05:39 PM
  3. how to make certain file types open IN your program
    By DarkViper in forum Windows Programming
    Replies: 4
    Last Post: 02-06-2003, 11:37 PM
  4. drawing over the open program.
    By Unregistered in forum Windows Programming
    Replies: 6
    Last Post: 01-23-2002, 03:37 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM