Jump to content
  • Advertisement
Sign in to follow this  
Simpsons4261

Question on the Hello World program...

This topic is 4080 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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;

Share this post


Link to post
Share on other sites
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++

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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].

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!