-
February 15th, 2009, 02:27 AM
#1
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
-
February 15th, 2009, 02:32 AM
#2
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.
-
February 15th, 2009, 02:37 AM
#3
Re: cin.get(); Doesn't Work for me... Anyone ??
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.
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.
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.
-
February 15th, 2009, 02:58 AM
#4
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.
-
February 15th, 2009, 03:01 AM
#5
Re: cin.get(); Doesn't Work for me... Anyone ??
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>.
-
February 15th, 2009, 08:54 PM
#6
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> ??
-
February 15th, 2009, 09:35 PM
#7
Re: cin.get(); Doesn't Work for me... Anyone ??
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|