cin.get(); V/s return 0;

Hi all,
I'm totally new in programming. I've been learning C++ since yesterday through online tutorials. I'm using Dev-C++ 4.9.9.2 on XP.

I have two concerns.

1. I copied "Hello world" coding from this website's tutorial and pasted them into C++. After clicking on "Compile & Run" button, the .exe just blinked and ended, within microseconds. I found another tutorial and at there all the codings were same except the return 0; function, it was replaced with cin.get();. I used cin.get(); in C++ and it worked fine. Now the .exe ends after I press enter. I tired other codings with return 0; and got the same useless results. But cin.get(); gives the desired results.
I want to why return 0; function runs this way on my system? And, will it make any problem if i continue to use cin.get(); function?

2. My second problem is related with Output coding. If I don't use cin.ignore(); function then the .exe ends just after i enter a number and press enter. I can't see the typed number. In this website's tutorial they didn't use cin.ignore(); function.

Thanks for your help, in advance.
cin.get() is a function that waits for you to enter something (which involves pressing Enter) and returns the first character from that. return is a statement that gives function a value that will be used where the function was called. These functions have nothing in common with each other. In fact your code should have both (get before return, as return exits the function). For some reason compilers allow omitting a return statement from main(), but in reality every non-void function should have one.

Your problem with entering numbers is because cin >> leaves the end of line character in the input stream which is found by your cin.get and so no pausing happens. Note that in this situation there is no difference between cin.get and cin.ignore - hey both look for a char to remove from the stream and wait if there are none (but there are situations where they can be used differently).

The reason why these things are not mentioned in the tutorial might be that since this is a console application, you should be running it from the command line (where the window won't close) or that some IDEs do the pausing for you (though mentioning these things would be a logical addition).

Speaking of IDEs, Dev-C++ is old and not going to be updated. You might want to try Code::Blocks insead.
Thanks for your help and the suggestion.
Topic archived. No new replies allowed.