Hi,
I'm new to C++.
When would I use cin.get() vs return 0?
And are there any other commonly used endings for the main() function?
Thanks
Hi,
I'm new to C++.
When would I use cin.get() vs return 0?
And are there any other commonly used endings for the main() function?
Thanks
What's different between
andCode:int main ( ) { cout << "hello world" << endl; cin.get(); return 0; }
andCode:int main ( ) { cout << "hello world" << endl; return 0; }
Is that those with cin.get() will wait for some user input.Code:int main ( ) { cout << "hello world" << endl; cin.get(); // return 0 is implied if not explicitly stated as returning a value }
It's a common way for programmers running their programs in poorly configured IDE's to stop the console window from disappearing at the end of the program.
In a normal console program, run normally from the command line, the extra cin.get() would become very annoying, very quickly to most users.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
"cin.get()" isn't common at all. At least not in anything more serious than a homework.
C++ has only notion of streams - if standard C++ is concerned, there is no concept of a console. By default, OS redirects standard input/output streams to a console window, and that's the only reason you see some console.