Results 1 to 5 of 5

Thread: Need help with C++ - can't get compiler to work properly

  1. #1
    Junior Member
    Join Date
    Jan 2010
    Posts
    4

    Question Need help with C++ - can't get compiler to work properly

    Hey

    So i've just started out with C++, using a guidebook I bought on amazon. I'm using bloodshed dev-C++, however, i can't get it to work properly. I'm following the tutorials very closely, and write exactly the code that i'm supposed to, however, when run/debug, the program closes itself before it's barely opened. I can only see it for a splitsecond. Anyone got a solution to this? I'd rather try not to get too messy and insert all kinds of code, since i'm new to it, and i'm trying to follow the book where i only have to put in some certain code, not all kinds of stuff i dont even understand what does....

    EDIT 1: Yeah, I did post the code, but it just takes forever to the moderator to approve it. Same reason I did two similar posts in the first place, thought they had forgotten about it...
    Anyways, here's the code:

    #include <iostream>
    using namespace std;

    int main()
    {
    cout<<"Hello World!";
    return 0;
    }
    Last edited by Isaac; 03-27-2012 at 04:19 AM.

  2. #2
    Senior Member
    Join Date
    Jun 2008
    Location
    Adelaide, Australia
    Posts
    126

    Default

    Without seeing the code, it's impossible to tell you what is happening. But, if I had to hazard a guess you are probably doing something like:

    Code:
    void main()
    {
      printf("Hello World");
    }
    In which case the program will print Hello World to the console and exit, closing the window. Using the getchar() or getch() functions should pause the program until you press a key.

    Another thought: Running from the command prompt (cmd.exe) will run the program and leave the output on the screen.

  3. #3

    Default

    Bloodshed is really old and unsupported, you get visual c++ express for free which is FAR better.

    in relation to your program you can also use system ("PAUSE") like this:

    Code:
    #include <stdlib.h>
    #include <iostream>
    int main ()
    {
        std::cout << "Hello World" << std::endl;
        system ("PAUSE");
    }

  4. #4
    Junior Member
    Join Date
    Mar 2012
    Posts
    10

    Default

    system ("PAUSE"); should work on your Dev++ compiler as well. At least it does for me.

    I've found that Dev compiler is decent for making simple programs for beginners such as myself, but the actual version you download from Microsoft is a bit sloppy for the newbs. Granted, I can program whatever code in either, I just prefer Dev if it is going to be a simple console app.

    Which reminds me, what do they mean when they say "polluting the namespace" in C++? And why is this such a frowned upon habit?

  5. #5
    Senior Member
    Join Date
    Feb 2009
    Location
    Finland
    Posts
    363

    Default

    I think people talk about "polluting namespace" in conjunction of writing "using namespace", which brings the content of the namespace to the namespace where "using namespace" resides, thus "pollutes" the namespace. Namespaces are used to avoid naming conflicts, thus namespace pollution causes potential conflicts. Personally I think it's fine to use "using namespce" in cpp scope and fix conflicts if such arise by removing "using namespace" and moving it to function scope or prefixing with name::. However you should't use "using namespace" in global scope of headers as it defeats the purpose.

Posting Permissions

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