CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Dec 2008
    Posts
    144

    [RESOLVED] Console Output Behaviour Question

    Not only am I new to these forums, I'm also new to Visual C++... I've got about 2 weeks under my belt now. I'm teaching myself through books and forums.

    I've just started working through the book C++ a Beginner's Guide, by Herbert Schildt, which is freely available and hosted by microsoft here.

    Right now I'm using the Visual Studio 2005 Command Prompt to create .cpp files, and then compile them. This is working fine. The issue is that when i run a compiled simple program the console window flashes up and then immediately disappears. I was expecting it to remain open so I could actually see the cout I wrote into the code.

    Here's a code example:

    #include <iostream>
    using namespace std;

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


    When I run the compiled version of the above code the console window flashes and immediately disappears. Is this normal behaviour? If not, what do I need to do to force the console to remain open so I can see it?

    NinjaEdit: I recently discovered this happens because of the 'return 0;' statement; that's why the console closes immediately. I also discovered that by calling the .exe from an already open console window I can see the output resulting from the program, and the console window will remain open.
    Last edited by CreganTur; December 1st, 2008 at 12:19 PM.

  2. #2
    Join Date
    Apr 2005
    Location
    Norway
    Posts
    3,934

    Re: [RESOLVED] Console Output Behaviour Question

    It's not the return statement that's causing the console window to close, it's is due to the fact that your program has come to an end.

    As you've noticed you can execute your application in a 'standalone' console window and the window will remain open. Another option is to start your application without debugging, and you're prompted with a "Press any key to continue..." when it comes to an end.

    - petter

  3. #3
    Join Date
    Apr 1999
    Posts
    27,449

    Re: [RESOLVED] Console Output Behaviour Question

    Quote Originally Posted by CreganTur View Post
    I've just started working through the book C++ a Beginner's Guide, by Herbert Schildt, which is freely available and hosted by microsoft here.
    I wonder how Schildt got that gig. He is one of the worst C++ authors out there (and this is not just me saying this -- most programmers who are familiar with Schildt knows all about the wrong information he publishes in his books)

    http://accu.org/index.php?module=boo...ws&func=search

    Enter as an author "Schildt". Check the number of "not recommended".

    Regards,

    Paul McKenzie

  4. #4
    Join Date
    Dec 2008
    Posts
    144

    Re: [RESOLVED] Console Output Behaviour Question

    Quote Originally Posted by Paul McKenzie View Post
    I wonder how Schildt got that gig. He is one of the worst C++ authors out there (and this is not just me saying this -- most programmers who are familiar with Schildt knows all about the wrong information he publishes in his books)

    http://accu.org/index.php?module=boo...ws&func=search

    Enter as an author "Schildt". Check the number of "not recommended".

    Regards,

    Paul McKenzie
    Well what should I do to avoid being corrupted by his taint?

    I've got access through work to Books24x7, and I've searched their C++ / Visual C++ titles pretty thoroughly... the only issue is that most of them give me crap about Visual C++ Express Edition not being good enough. Buying a compiler is not an option, since I'm just beginning; that and work won't pay for it.

    I will say that I've noticed a lot of bad-practice in his code examples (I'm pretty proficient in VBA, so I have some programming experience), but I put them down to him trying to be accessible to non-programmers/total noobs. And even though he may not be the best I've learned a good deal so far.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured