Thread: Hello Program

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    1

    Hello Program

    I am just starting out with my C++ Programming. I got this book Sam's Teach yourself C++ in 21 days. Im making the hello program. When I run after its compiled and stuff, It pops up, shows for like a second or less and minimizes or closes. Here is the code. If there is a way to fix it please tell me and please explain to me what it does.

    #include <iostream.h>

    int main()
    {
    cout << "Hello World!\n";
    return 0;
    }

    Thank you

  2. #2
    Registered User loopy's Avatar
    Join Date
    Mar 2002
    Posts
    172

    I have a solution

    From: http://www.cprogramming.com/close.html
    "It is a common problem for your program's output window to close before you can see the result of the program. The simplest solution is to require the program to wait for the user to input a final keypress before closing. The simplest solution is to add the following code at the end of your program, just before the return 0;:
    cin.get();

    This code will cause your compiler to wait until you press a key before closing the program's output window."

    Another thing i notace is there's no need for a \n (newline)

    Hope that help's.
    WorkStation(new, a month ago):

    Sony Vaio i686 Desktop
    2.60 GIGhz Intel Pentium 4(HT)
    512Mb DDR RAM
    800MHz Front Side Bus!
    120 GIG IDE HardDrive
    Matrox G400 Dual-Head
    Linux kernel 2.6.3
    Modified Slackware 9.1
    GCC/GDB

    Multi-mon
    Simultaneous Multiple Processes

  3. #3
    Registered User xlnk's Avatar
    Join Date
    Mar 2002
    Posts
    186
    you might want to add system("cls"); to clear the screen also, you could use getch() to pause the screen, for example:

    Code:
    #include <iostream.h>
    #include <stdlib.h>
    #include <stdio.h> 
    
    int main() 
    { 
        system("cls");   // you can use clrscr() if your using dos compiler
        cout << "Hello World!\n"; 
        getch();
        return 0; 
    }
    the best things in life are simple.

  4. #4
    _B-L-U-E_ Betazep's Avatar
    Join Date
    Aug 2001
    Posts
    1,412
    when you are making console applications (at least when you are beginning), you should get out of the whole "point & click" mentality.

    When you p&c, windows controls/opens your console, runs the code, and then shuts the console down (by default for 95/98/ME and perhaps XP OSes).

    Open your console window first from your start menu and type the path/name of your executable.

    Then you will see your display....

    if my program is in C:\computer_newbie\bin\ and it is named hello.exe and my prompt looks like this....

    C:\>

    then I type

    C:\>cd .\computer_newbie\bin

    C:\computer_newbie\bin\>hello.exe

    Hello world

    C:\computer_newbie\bin\>

    These are console applications. Learn how to use the console (dos) before following the standard advice of "pausing" your program to keep in the point & click mentality................. IMHO
    Blue

  5. #5
    Registered User xds4lx's Avatar
    Join Date
    Nov 2001
    Posts
    630
    Hmm, some compilers have the option to Run-Captured, which means it will keep the DOS box open when the code execution ends. But this is compiler specific.
    "only two things are infinite, the universe and human stupidity, and im not sure about the former." - albert einstein

  6. #6
    _B-L-U-E_ Betazep's Avatar
    Join Date
    Aug 2001
    Posts
    1,412
    Win NT and WIN2000 keep the console open after execution which is OS specific....
    Blue

  7. #7
    Registered User rmullen3's Avatar
    Join Date
    Nov 2001
    Posts
    330
    You don't need to use cls. Just put a system() call with pause

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 01:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM