Solved

Can't get basic C++ program to run.

Trying to learn C++ and starting by doing the basic Hello World program.

But it doesn't run, it's very basic obviously:

#include <iostream>
using namespace std;

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


I'm using Code Lite as my IDE and ive trying running with with both the GNU GCC compiler, GNU G++ compiler and it has not changed. I'm thinking i'm going to need to use a different compiler but i do not know how to go about getting it.
16 answers Last reply Best Answer
More about basic program
  1. What error message does it give? Or is it just that you don't see the output? This is common when using an IDE. Just run from the command line or else add a cin statement just before the return; this will pause the program, waiting for input, so that you can see the output.
  2. I reckon the problem is as Ijack mentioned: it runs, but you see no output because it finishes executing too quickly.
  3. Same here. Just run the program from the command line or add a line to pause it.
  4. you can take idea's from W3schools.com
  5. The only thing that happens when i execute it is a window pops up with "Press any key to continue" and then the output just says "Program exited with return code: 0"

    This is what is says under the "Build" tab":


  6. It's also possible that the problem is that the cout buffer isn't being flushed. Try adding:

    << endl

    to the end of your bout line.
  7. Ijack said:
    It's also possible that the problem is that the cout buffer isn't being flushed. Try adding:

    << endl

    to the end of your bout line.


    Like this?:

    #include <iostream>
    
    int main ()
    using namespace std;
    {
    << cout "Hello World" << endl;
    
    return 0;
    } 


    A window still pop up with "Press any key to continue" and then i get this output:

  8. The line should read:

    cout << "Hello World" << endl;

    If it does then I don't know why you're not getting any output. Have you tried running the program from a command prompt?
  9. Ijack said:
    The line should read:

    cout << "Hello World" << endl;

    If it does then I don't know why you're not getting any output. Have you tried running the program from a command prompt?


    The Windows command prompt?
  10. Yes. Open a Windows command prompt and try running from there.
  11. Best answer
    probably the program is finishing to fast.. just add a something like that to wait for user input.

    int x;
    cin >> x;

    before the return 0; code

    if no console window is open try using windows prompt like @Ijack advised u.
  12. ricardois said:
    probably the program is finishing to fast.. just add a something like that to wait for user input.

    int x;
    cin >> x;

    before the return 0; code

    if no console window is open try using windows prompt like @Ijack advised u.


    I switched over to Dev C++ and the code now executes, but then the window was closing to fast but i inserted those lines into the code and the window now stays open. So do i have to keep inserting that code into other projects for them to stay open?

    Thanks for the help guys, very very good advice. :D
  13. there is a execute mode that keeps the window open when the program finishes, but it depends on what program you are using.

    you could also try this:

    system( "PAUSE" );

    on the end, this will keep the window open...

    about this system(); since you are learning, this will execute a string like a normal console command, example system("cls"); will clear the console window. very cool indeed xD
  14. Best answer selected by CDdude55.
  15. ricardois said:
    there is a execute mode that keeps the window open when the program finishes, but it depends on what program you are using.

    you could also try this:

    system( "PAUSE" );

    on the end, this will keep the window open...

    about this system(); since you are learning, this will execute a string like a normal console command, example system("cls"); will clear the console window. very cool indeed xD


    Awesome!, thanks a lot. :D
  16. CDdude55 said:
    So do i have to keep inserting that code into other projects for them to stay open?

    Yes, because the program only does what you tell it to, nothing more.

    Use cin.get() (possibly preceded by cin.ignore()) rather than system("pause"). The latter is a bad idea in 100 different ways, starting from the fact that by using it you're calling another process, which is a security issue.
Ask a new question

Read More

Programming Compile Basic Gnu Apps