program not staing open

This is a discussion on program not staing open within the C++ Programming forums, part of the General Programming Boards category; im very new to c++ so i might be missing something but when i try to run a very simple ...

  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
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  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,479
    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
    I'm just trying to be a better person - My Name Is Earl

  5. #5
    -JM
    -JM is offline
    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,211
    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, 08:07 PM
  2. File Types Automatically Open With My Program
    By ElWhapo in forum Windows Programming
    Replies: 3
    Last Post: 12-29-2004, 04: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, 10:37 PM
  4. drawing over the open program.
    By Unregistered in forum Windows Programming
    Replies: 6
    Last Post: 01-23-2002, 02:37 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

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