Thread: Using cin.get() vs return 0

  1. #1
    Registered User
    Join Date
    Mar 2016
    Posts
    18

    Using cin.get() vs return 0

    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

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    What's different between
    Code:
    int main ( ) {
      cout << "hello world" << endl;
      cin.get();
      return 0;
    }
    and
    Code:
    int main ( ) {
      cout << "hello world" << endl;
      return 0;
    }
    and
    Code:
    int main ( ) {
      cout << "hello world" << endl;
      cin.get();
      // return 0 is implied if not explicitly stated as returning a value
    }
    Is that those with cin.get() will wait for some user input.

    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.

  3. #3
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    733
    Quote Originally Posted by Jasper Dunn View Post
    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
    "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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 07-04-2007, 12:20 AM
  2. Replies: 12
    Last Post: 04-07-2007, 11:11 AM
  3. Replies: 6
    Last Post: 04-09-2006, 04:32 PM
  4. Replies: 4
    Last Post: 07-15-2005, 04:10 PM
  5. Return Return Error
    By javacvb in forum C++ Programming
    Replies: 8
    Last Post: 12-16-2003, 04:17 PM