Hello World Closes Automatticaly HELP!!!

This is a discussion on Hello World Closes Automatticaly HELP!!! within the C++ Programming forums, part of the General Programming Boards category; This is my first little program and everything looks good, but the problem I got is That the cmd window ...

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    8

    Hello World Closes Automatticaly HELP!!!

    This is my first little program and everything looks good, but the problem I got is That the cmd window closes by itself, This is the code I am using from the Book Accelerated C++

    I always get thismessage "The program '[5676] project.exe: Native' has exited with code 0 (0x0)."

    Code:
    #include <iostream>
    
    int main()
    {
    	std::cout <<"Hello World" << std::endl;
    	return 0;
    }

    I am using Visual C++ 2010 express. Win 7 64bit.

    Thank you

  2. #2
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    use
    system("pause") ;
    before return 0;

    The message you get means your code compiled successfully.
    No mystery there.

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    8
    Quote Originally Posted by Eman View Post
    use
    system("pause") ;
    before return 0;

    The message you get means your code compiled successfully.
    No mystery there.
    ok,

    but I read someplace in these forums that using system("pause") is not a good practice.

  4. #4
    Registered User
    Join Date
    Oct 2010
    Posts
    8
    Quote Originally Posted by Eman View Post
    use
    system("pause") ;
    before return 0;

    The message you get means your code compiled successfully.
    No mystery there.
    IT DOES WORK!!! THANK YOU.

    WHAT ABOUT

    system("pause") ;

    is it good to use it.

  5. #5
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Quote Originally Posted by BadAssjoe View Post
    ok,

    but I read someplace in these forums that using system("pause") is not a good practice.
    It's not a good practice.

    Use cin.get() to wait for another character to be pressed before having the window closed.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  6. #6
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    The reason system("pause") is wrong can be found here.

    System("pause"); - Why is it wrong? - Stack Overflow
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  7. #7
    Registered User
    Join Date
    Oct 2010
    Posts
    8
    Quote Originally Posted by claudiu View Post
    It's not a good practice.

    Use cin.get() to wait for another character to be pressed before having the window closed.
    cin.get() before return 0;

    gives me a error. do I need to include# < ????>

    Thank you

  8. #8
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Oh sorry, I guess in your program it would be std::cin.get(), since you haven't included the standard namespace. (don't worry about what that means if you are a beginner ).
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  9. #9
    Registered User
    Join Date
    Oct 2010
    Posts
    8
    Quote Originally Posted by claudiu View Post
    Oh sorry, I guess in your program it would be std::cin.get(), since you haven't included the standard namespace. (don't worry about what that means if you are a beginner ).
    Nope still error

    Code:
    #include <iostream>
    
    int main()
    {
    	std::cout <<"Hello World" << std::endl;
    	std::cin.get();
    	return 0;
    }
    Thanks again

  10. #10
    Registered User
    Join Date
    Oct 2010
    Posts
    8
    Quote Originally Posted by BadAssjoe View Post
    Nope still error

    Code:
    #include <iostream>
    
    int main()
    {
    	std::cout <<"Hello World" << std::endl;
    	std::cin.get();
    	return 0;
    }
    Thanks again
    Sorry I DID WORK!

    I did not rebuild it.


    Thanks

  11. #11
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    I didn't know that system("pause") was a bad idea sorry. Just starting C++ myself.

    What I know though is, that using cin.get() does not always work.
    if I use I use 3 cin.get() it never works at times.
    But system("pause") always works for me. which was why I recommended it.
    Anyways glad to be of some help in a way

  12. #12
    Registered User
    Join Date
    Oct 2010
    Posts
    8
    Quote Originally Posted by Eman View Post
    I didn't know that system("pause") was a bad idea sorry. Just starting C++ myself.

    What I know though is, that using cin.get() does not always work.
    if I use I use 3 cin.get() it never works at times.
    But system("pause") always works for me. which was why I recommended it.
    Anyways glad to be of some help in a way
    Thanks, man.

    Now I know two different ways to do it.

  13. #13
    Registered User
    Join Date
    Jan 2009
    Location
    Sweden
    Posts
    8
    Another way that removes the need for cin.get() or system("pause") is that you use the command prompt (CMD) if you use windows or the terminal if you use unix/linux/mac. Navigate to the folder where your compiled program is and run it through the command prompt or terminal depending on your operating system and you will see all the output until you close the command prompt / terminal.

    (A trick on Windows to make it easier, shift + right click on the folder with your program inside and chooce open with command prompt here and you donīt have to use cd to navigate there)

  14. #14
    The Autodidact Dante Wingates's Avatar
    Join Date
    Apr 2010
    Location
    Valhalla
    Posts
    56
    Quote Originally Posted by Eman View Post
    I didn't know that system("pause") was a bad idea sorry. Just starting C++ myself.

    What I know though is, that using cin.get() does not always work.
    if I use I use 3 cin.get() it never works at times.
    But system("pause") always works for me. which was why I recommended it.
    Anyways glad to be of some help in a way


    Then use cin.ignore() to ignore the nl that is bothering you

    This code fragment wont work as you might expect:

    Code:
    #include <iostream>
    
    int main()
    {
        char c;
        std::cout << "Type a character: ";
        c = std::cin.get();
        std::cout << "You typed: " << c;
        std::cin.get();
    
        return(0);
    }
    But this one will:

    Code:
    #include <iostream>
    
    int main()
    {
        char c;
        std::cout << "Type a character: ";
        c = std::cin.get();
        std::cin.ignore();
        std::cout << "You typed: " << c;
        std::cin.get();
    
        return(0);
    }
    Last edited by Dante Wingates; 10-20-2010 at 04:40 PM.
    2B OR !2B? That is the question!

  15. #15
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    IDE have options some times to keep the window open. In any case, whatever works, these are test codes anyway.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. The 7 New Wonders of the World
    By Mario F. in forum A Brief History of Cprogramming.com
    Replies: 36
    Last Post: 12-08-2006, 12:55 PM
  2. Obfuscated Code Contest: The Results
    By Stack Overflow in forum Contests Board
    Replies: 29
    Last Post: 02-18-2005, 04:39 PM
  3. Converting from Screen to World Coordinates
    By DavidP in forum Game Programming
    Replies: 9
    Last Post: 05-11-2004, 12:51 PM
  4. Too much to ask ?
    By deflamol in forum C Programming
    Replies: 2
    Last Post: 05-06-2004, 04:30 PM
  5. No More Technology After World War Three
    By Unregistered in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 09-20-2001, 07:02 PM

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