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

Status
Not open for further replies.

CDdude55

Distinguished
Dec 15, 2011
29
0
18,590
Trying to learn C++ and starting by doing the basic Hello World program.

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

Code:
#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.

 
Solution
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.

Ijack

Distinguished
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.
 

CDdude55

Distinguished
Dec 15, 2011
29
0
18,590
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":


build%20error.png

 

CDdude55

Distinguished
Dec 15, 2011
29
0
18,590


Like this?:

Code:
#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:

output.png


 

CDdude55

Distinguished
Dec 15, 2011
29
0
18,590


The Windows command prompt?

 

ricardois

Distinguished
Dec 21, 2011
14
0
18,570
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.
 
Solution

CDdude55

Distinguished
Dec 15, 2011
29
0
18,590


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
 

ricardois

Distinguished
Dec 21, 2011
14
0
18,570
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
 

CDdude55

Distinguished
Dec 15, 2011
29
0
18,590


Awesome!, thanks a lot. :D
 

randomizer

Distinguished

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.
 
Status
Not open for further replies.