Question on the Hello World program...

Started by
8 comments, last by Aardvajk 16 years, 8 months ago
Yes I'm a noob, lol. The hello world program isn't working. I'm using Dev C++, and I'm using the cplusplus.com tutorial. Here is my code: // my first program in cpp #include <iostream> using namespace std; int main () { cout << "Hello World!"; return 0; } The program doesn't even come up on the screen. Its odd that it isn't working even though the tutorial is telling me to do that. Are there any better online tutorials than this site or am I just missing something really obvious?
Advertisement
lol, I use to be right where you were a little bit ago using those tutorials(BTW they arent very good after a while) and I had the same concern. The problem is(accutally its not a problem) that the program is running line by line and unless there is line of code that asks the users for anything the code will just execute, displaying the words Hello World and then exiting. To see if you are getting the desired out put write 'system("pause")' w/o the single quotes. This will stop the program from automatically quitting out after all the code has been interpretted.

****You must put that piece of code in, instead of return 0;
It is working. When you run a console application from outside the console, it will open a window, run the app, and close it again.

There are several ways around this:
1) Run the program from a console window.
2) In Visual Studio, run without debugging (ctrl+f5).
3) Set a breakpoint at your return statement. When it is hit, switch to the console window.

Edit: Just noticed you are using Dev-C++. I recommend against it, in favor of MS Visual Studio. The Express Edition of 2005 is free, feature-rich, and much more up to date than Dev-C++
Try this:
#include <iostream>using namespace std;int main (){   cout << "Hello World!";   // Wait for keypress   int c = cin.get();   cin.get();   return 0;}


I suspect that the program is running fine, however, when the program
is done executing, it immediately terminates. Because the code executes
very fast, it seems as if nothing happens.

The above code will stay open until you press a key.

Hope this helps[smile]
The code looks correct to me. What's happening when you compile and run it? Is a console window popping up and closing real fast?

If so, it is running, but upon terminating it's exiting the program, which causes the console window to close. In Visual C++ 2005 Express you can use CTRL+F5 to make the window stay up after termination. Not sure about Dev-C++.

Quite a few people will tell you to throw a getch() call or something as the last line to make the program stay open, but I don't recommend that method, for a number of reasons.

Cheers!
Jeromy Walsh
Sr. Tools & Engine Programmer | Software Engineer
Microsoft Windows Phone Team
Chronicles of Elyria (An In-development MMORPG)
GameDevelopedia.com - Blog & Tutorials
GDNet Mentoring: XNA Workshop | C# Workshop | C++ Workshop
"The question is not how far, the question is do you possess the constitution, the depth of faith, to go as far as is needed?" - Il Duche, Boondock Saints
Quote:Original post by JWalsh
Quite a few people will tell you to throw a getch() call or something as the last line to make the program stay open, but I don't recommend that method, for a number of reasons.


Seconded. Artificial pauses (via System("pause"), getch(), cin.get(), or whatever else) are bad practice. You are making a console application, which is meant to be run from the console. Run it as intended and you won't have any problems.
Quote:Original post by Driv3MeFar
Edit: Just noticed you are using Dev-C++. I recommend against it, in favor of MS Visual Studio. The Express Edition of 2005 is free, feature-rich, and much more up to date than Dev-C++


Should I just use the C++ edition?
In my personal opinion, there are no better IDE's for personal application development and/or learning than the Visual Studio 2005 Express Editions (C# EE, C++ EE, VB.NET EE)

They are free, configurable, support the latest language standards supported by the majority of compilers, and all 3 implement the best interactive debuggers on the planet.

If more people knew how to use their debugger to step through their programs line by line, and observe local and automatic variables and memory, there would be far fewer posts on 'For Beginners' about "why does my program not work."

Plus, thanks to MSDN, Microsoft's implementation if C++ has some of the best reference documentation available for class libraries and methods.

Just my opinion, mind you. And while people may disagree with me, it doesn't change the fact that I recommend the Express Editions, nor does it change the fact that I use them myself whenever possible.

Cheers!
Jeromy Walsh
Sr. Tools & Engine Programmer | Software Engineer
Microsoft Windows Phone Team
Chronicles of Elyria (An In-development MMORPG)
GameDevelopedia.com - Blog & Tutorials
GDNet Mentoring: XNA Workshop | C# Workshop | C++ Workshop
"The question is not how far, the question is do you possess the constitution, the depth of faith, to go as far as is needed?" - Il Duche, Boondock Saints
Quote:
In my personal opinion, there are no better IDE's for personal application development and/or learning than the Visual Studio 2005 Express Editions (C# EE, C++ EE, VB.NET EE)

Seconded.

I personally dont mind using cin.get() simply because it is still ANSI C++,
and is very easy to impliment. This is my opinion, though.

I do, however, recommend not using getch()/getche() and system() functions
though.
cin.get() can get confused by a program that uses cin to read input in the main chunk of the code. If a character is left in the input buffer, the cin.get() at the end can appear to "fail".

I've seen this confuse beginners and for that reason, given that the artificial pause is only ever a temporary fix and standards compliance is really not an issue here, getch() (or _getch() with VS) is a better way to wait for a key.

I've not yet come across a C or C++ compiler that does not provide a non-standard <conio.h> and getch().

Having said that, for writing simple console applications, I still maintain that there is a hell of a lot to be said for learning to use a command-line compiler, MAKE and EDIT. I spent years writing code like that before I started using these new-fangled IDEs [smile].

This topic is closed to new replies.

Advertisement