Sign in to follow this  
Emotions06

Hello World problem

Recommended Posts

Emotions06    122
Okay, yes I know this sounds a little bad, but I had to ask for help somewhere, why not here? So I gave programming a go a while back and know the basics (cout, cin, variables, if statements, etc.) and have decided to give it a go once again. I am going to start from the bottom again to refresh what I have already learned, and also so that when I get to something I didn't understand the first time I may have a better chance succeeding this time. Well it's a good thing I started from the beginning again, because after reading a tutorial (cplusplus.com) and doing the basic "Hello World" beginner program, I could get it to actually show up. I mean I am very sure I done the coding right (real hard right?=P), but it would compile and run, but it would just flash up, when in reality I would like for it to stay there so that I can actually see what it was I typed. How can I achieve this? I used to know, but have forgotten and need a little reminder. Also if my memory serves me right what I was told was kinda controversial and some people said I shouldn't do it a certain way, but I do remember something about cin.get ( possibly??).. But just a little help on how I can get that working would be great. Emotions I always seem to ramble. Sorry.

Share this post


Link to post
Share on other sites
What happens, is you tell the computer to display text to the console, but you then tell it to exit a second later. ('return 0;') What you need is some kind of delay.
'System("pause");' is the common use for hello world, but that's not recomended due to the fact that it is actaully running a different program (Named "pause") and could be mistakenly replaced. A better idea is to add a delay, (#include <windows.h>, 'Sleep(2000);') or to create a loop the program gets stuck in, until you recieve input. ('cin.get()') Or you could just run your program in the command box.

If you deside to use 'System("pause");' or 'Sleep(2000);' or any other delay, make sure you place it after you write you text with cout, and before you quit with 'return 0;'

Share this post


Link to post
Share on other sites
erissian    727
You could call it from the command line, or if you're using a link there should be an option to "Don't close this window" or some such thing.

Share this post


Link to post
Share on other sites
void*    292
It sounds like you're running it from inside an IDE and it's opening a window for you, which it's then closing immediately. Usually there's a way around this (such as an item in the preferences as erissian mentioned). Often there's also a shortcut for doing this. For example, in Visual Studio you can run it and have the window stay opened by using Ctrl-F5 instead of just F5.

Which IDE are you using? Or, are you running it from Start->Run...? With a little more information about how you're running the program, we can help you view the message longer without having to actually change your code, just by running it in a different way.

When you get to more advanced stuff, you'll probably want to avoid some of the methods mentioned by Servant of the Lord, although they will work fine for now... (Servant's suggestion of running it in a console is a good way of doing it, also, but it takes you away from your IDE to do so, and that can get annoying).

In the long term, when you start working within an Input/Output loop, it won't matter that the window closes immediately after you're finished, because you won't be finished until after a lot of interaction, so after learning a few more things you don't have to worry about it.

Share this post


Link to post
Share on other sites
CaptCanada    224
Hi,

I know what you are going through. I have started/stopped learning programming, and now more specificially, game programming more times than I can count.

I decided this time to learn "only" what I need for game programming, whereas before I was trying to "take it all in". It is going much better this time around.

Back to you question, I think that using "cin.get();" would be a good way to go. It works in for me in Visual C++ Express. Of course, the tip about using Ctrl-F5 instead of F5 is great. I never knew about that shortcut, but was something I hoped would be available rather than adding code in your program to keep the window open.

Share this post


Link to post
Share on other sites
benryves    1999
Console programs should really exit cleanly and not have to wait for a keypress, so if using the cin.get() trick check to see if it's the debug build or not.

Personally, I stick a breakpoint on the closing brace. That's no extra code and debugging works merrily (unlike Ctrl+F5).

Share this post


Link to post
Share on other sites
Aardvajk    13207
While I would completely agree that waiting for a keypress at the end of a console application is not good practise, I accept fully that beginners are going to keep doing this and I don't see the harm on "learning" projects.

What I would suggest, though, is if using Visual C++, rather than cin.get(), which can be "fooled" by data being left in the input buffer following on from previous "legitimate" input operations, beginners are better advised to #include <conio.h> and _getch(), which is not standard but will actually physically wait for a keypress.

There is normally an equivalent function with other compilers, normally just called getch() and normally in <conio.h>. Actually, getch() works with VS as well but you get annoying warnings about deprecated functions.

Share this post


Link to post
Share on other sites
Emotions06    122
I'm sorry but what do you mean by IDE? Do you mean compiler? I went and downloaded the Dev-C++ compiler from bloodshed. Should I consider getting a different one or will this suit my learning needs?

Thanks I tried all of those out and got it working correctly.

Emotions

Share this post


Link to post
Share on other sites
Werefox    122
IDE means 'Integrated Development Environment'. Microsofts Visual Studio program is an example of this, as are things like ActiveState's Komodo (primarily perl), and the Eclipse framework (defaults as a Java IDE, but can run plugins for other languages including C/C++).

If you are not familiar with these, an IDE often has alot of features that ease some of the burden of developing code. Things like syntax highlighting, adding a graphical interface for your debugger or compiler, are some of the more common features. Thats not to say you can't program in something akin to windows notepad, or unix's vi, and just use commandline compiler and debugger - infact some people prefer to program in that way.

I'm not familiar with the Dev-C++ compiler, and I lack the time this morning to go look, so I can't really add a valid opinion about its suitiblity.

Hope that helps alittle.

Share this post


Link to post
Share on other sites
qux    122
From bloodshed.net:

Quote:

Bloodshed Dev-C++ is a full-featured Integrated Development Environment (IDE) for the C/C++ programming language. It uses Mingw port of GCC (GNU Compiler Collection) as it's compiler. Dev-C++ can also be used in combination with Cygwin or any other GCC based compiler.


So Dev-C++ is the IDE, GCC is the compiler. I'm not picking or anything, just thought i'd add some clarification. In regards to the original post, you'll probably find waiting for input the easiest solution.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this