Sign in with
Sign up | Sign in
Your question
Solved

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

Last response: in Apps General Discussion
Share
April 19, 2012 12:35:56 AM

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

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

  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main ()
  5. {
  6. cout << "Hello World!";
  7. return 0;
  8. }


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.

More about : basic program run

a b L Programming
April 19, 2012 6:30:20 AM

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.
m
0
l
a b L Programming
April 19, 2012 9:45:23 AM

I reckon the problem is as Ijack mentioned: it runs, but you see no output because it finishes executing too quickly.
m
0
l
Related resources
April 19, 2012 11:41:19 AM

Same here. Just run the program from the command line or add a line to pause it.
m
0
l
April 19, 2012 12:41:51 PM

you can take idea's from W3schools.com
m
0
l
April 19, 2012 12:57:24 PM

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":



m
0
l
a b L Programming
April 19, 2012 2:36:20 PM

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.
m
0
l
April 19, 2012 8:06:22 PM

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?:

  1. #include <iostream>
  2.  
  3. int main ()
  4. using namespace std;
  5. {
  6. << cout "Hello World" << endl;
  7.  
  8. return 0;
  9. }


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



m
0
l
a b L Programming
April 19, 2012 8:40:47 PM

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?
m
0
l
April 19, 2012 9:11:20 PM

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?

m
0
l
a b L Programming
April 20, 2012 6:06:51 AM

Yes. Open a Windows command prompt and try running from there.
m
0
l

Best solution

April 20, 2012 6:29:19 PM

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.
Share
April 20, 2012 9:18:04 PM

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 
m
0
l
April 20, 2012 10:17:17 PM

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
m
0
l
April 21, 2012 1:26:38 AM

Best answer selected by CDdude55.
m
0
l
April 21, 2012 1:27:28 AM

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 
m
0
l
a b L Programming
April 21, 2012 2:50:29 AM

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.
m
0
l
!