new, needs help

This is a discussion on new, needs help within the C++ Programming forums, part of the General Programming Boards category; Hi everyone. I just got a compiler, and I made a "Hello World" type of program, but when I run ...

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    4

    Unhappy new, needs help

    Hi everyone. I just got a compiler, and I made a "Hello World" type of program, but when I run it, I get a black screen that pops up for about a second, then it disappears. Help.

  2. #2
    Registered User
    Join Date
    Oct 2002
    Posts
    155
    post your code?

  3. #3
    Registered User
    Join Date
    Nov 2002
    Posts
    4
    I think the code's right, because I used it straight from a guide, and I can compile it without errors, but here goes.


    #include <iostream.h>

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



    The problem comes to when I run it. A DOS screen pops up, disappears, and that's it.

  4. #4
    UNBANNED OneStiffRod's Avatar
    Join Date
    Jan 2002
    Posts
    669
    Code:
    #include <iostream>
    #include <conio.h>  //I think this is the one with getch()
    
    using namespace std;
    
    int main()
    {
    cout << "Hello World!\n";
    cout<<"Press any key to continue"<<endl;
    getch();
    return 0;
    }
    Last edited by OneStiffRod; 11-16-2002 at 02:05 PM.
    My Avatar says: "Stay in School"

    Rocco is the Boy!
    "SHUT YOUR LIPS..."

  5. #5
    Registered User
    Join Date
    Nov 2002
    Posts
    4
    Yeah, that works. Why was that stuff added? Just linking me to an explanation would be fine.

  6. #6
    Registered User
    Join Date
    Nov 2002
    Posts
    14
    getch() waits for a keypress i think , You could also just run your program in the command prompt. Start-> Run -> Command and then execute your program with that. When running the executable strait off, it does what its sopose to do then quits.

  7. #7
    UNBANNED OneStiffRod's Avatar
    Join Date
    Jan 2002
    Posts
    669
    conio.h is the DOS header I beleive - and it contains a function called getch() which waits for a user to press a KEY.

    after the key is pressed your program will continue which in this case returns 0; and your program exits.

    All C++ programs act this way, once main() exits or returns a value the program ends, so you need to place something in between the return; statement if you don't want your program to end so quickly.
    My Avatar says: "Stay in School"

    Rocco is the Boy!
    "SHUT YOUR LIPS..."

  8. #8
    Registered User
    Join Date
    Nov 2002
    Posts
    4
    Ok, thanks. That really should be in the beginner's manuals.

  9. #9
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    Conio is nonstandard... better to vouch for something along the lines of:

    Code:
    #include <iostream>
    
    int main()
    {
        using namespace std;
    
        cout << "Hello World!\n";
        cin.get();
    
        return 0;
    }

  10. #10
    moi
    moi is offline
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    better better way is to run in a real console window that dumps one back to shell prompt after termination.
    hello, internet!

Popular pages Recent additions subscribe to a feed

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