CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Feb 2009
    Posts
    80

    Angry cin.get(); Doesn't Work for me... Anyone ??

    Hey everyone,
    been working hard to get past the newbie stage ! However I have yet one more totally newbie question.

    when I use cin.get(); at the end of a program or the end of a function to keep the prompt window open.....

    IT DOESN'T EVER WORK ~!!

    I see it in everyone elses code used the same way...... I think.

    here's an example. when I run even the ol' simple " hello world " program with cin.get(); at the end, it won't stay open. The program runs, and the window closes in about 0.343 seconds.

    here's a quick example code:

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        cout << "Hello world!" << endl;
        return 0;
    
        cin.get();
    }

    Am I using it correctly ?

    if so why doesn't this work for any of my programs ?

    thanks

  2. #2
    Join Date
    Feb 2009
    Posts
    80

    Re: cin.get(); Doesn't Work for me... Anyone ??

    ok ok ok,,,,
    I get it...

    it's the return 0; right ? can someone steer me to an article or tutorial about the use of

    " return " because I'm not clear on it's use.

    Won't a C++ program automatically return 0 if it was successful ?

    why use it ?


    thanks.

  3. #3
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: cin.get(); Doesn't Work for me... Anyone ??

    Quote Originally Posted by Jeff++
    it's the return 0; right ?
    Yes, you probably want to swap the order of the cin.get() call and the return statement.

    Alternatively, ditch the cin.get() call and run your program from the command prompt, or from an IDE that automatically pauses the program at the end, or by setting a break point with your debugger.

    Quote Originally Posted by Jeff++
    Won't a C++ program automatically return 0 if it was successful ?
    Yes, the global main function returns 0 if control reaches its end without encountering a return statement.

    Quote Originally Posted by Jeff++
    why use it ?
    Some people just want to be consistent with other functions since the global main function is special in this regard.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  4. #4
    Join Date
    Feb 2009
    Posts
    80

    Re: cin.get(); Doesn't Work for me... Anyone ??

    so....

    a function WON'T return true or false unless that line is at the end of that function ?

    can you use more than 0 and 1 ?

    like..... number each function with a return number higher than the last one... so if there's an error you know what function it was ?

    thanks.

  5. #5
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: cin.get(); Doesn't Work for me... Anyone ??

    Quote Originally Posted by Jeff++
    can you use more than 0 and 1 ?
    Since the return type is int, you can return any int value. However, in the case of the global main function you should only return 0, EXIT_SUCCESS or EXIT_FAILURE. The latter two are macros defined in <cstdlib>.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  6. #6
    Join Date
    Feb 2009
    Posts
    80

    Re: cin.get(); Doesn't Work for me... Anyone ??

    cool. thanks laserlight.

    how does one know what functions or macros are defined in what header files ?

    and why do some need the .h on the end like <windows.h> and some don't like <iostream> ??

  7. #7
    Join Date
    Feb 2009
    Posts
    56

    Re: cin.get(); Doesn't Work for me... Anyone ??

    Books and learns

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured