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

Discussion in 'Game Development (Technical)' started by Isaac, Mar 25, 2012.

  1. Isaac

    Isaac
    Expand Collapse
    New Member

    Joined:
    Jan 28, 2010
    Messages:
    4
    Likes Received:
    0
    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;
    }
     
    #1 Isaac, Mar 25, 2012
    Last edited: Mar 27, 2012
  2. Stropp

    Stropp
    Expand Collapse
    New Member

    Joined:
    Jun 25, 2008
    Messages:
    121
    Likes Received:
    0
    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. jamesm

    jamesm
    Expand Collapse
    New Member

    Joined:
    Jul 28, 2008
    Messages:
    48
    Likes Received:
    0
    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. Ludiologist

    Ludiologist
    Expand Collapse
    New Member

    Joined:
    Mar 22, 2012
    Messages:
    10
    Likes Received:
    0
    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. JarkkoL

    JarkkoL
    Expand Collapse
    New Member

    Joined:
    Feb 4, 2009
    Messages:
    363
    Likes Received:
    0
    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.
     

Share This Page