i know ther is a way to make the program stop and wate for you to press a butten like enter
the bad thing is i dont know what this is can someone post it?
Printable View
i know ther is a way to make the program stop and wate for you to press a butten like enter
the bad thing is i dont know what this is can someone post it?
Use system("pause"); This is how I do it. Or include conio.h and use getch();
This is the C++ forum bro :PQuote:
Originally Posted by Marlon
If youve used cin in your program before and need to pause, youll need to put cin.ignore(); before that cin.get(); and it'll pause.Code:#include <iostream> // the library for cin, and cout
using namespace std; // the namespace used from iostream
cin.get(); // pauses the program
This is probably in the FAQ as the above said.
cin.get(); only pauses after user input though doesn't it? :confused:Quote:
Originally Posted by Dae
It'll pause UNTIL user input.
system("pause") is often a bad idea because there is always the possibility that pause.exe doesn't exist, for a number of reasons.
Despite being non-standard, conio.h is a very commonly used library. Personally, getch() is a function I'd definitely like to have if I was working with a console application.Quote:
This is the C++ forum bro :P
I might do that... Maybe.
You might do what?
What you said.
I didn't tell you to do anything.
cin.get() will sit and wait until the user hits the return key.
You're probably better off using a function that read keystrokes directly. (eg getch() which lives in <conio.h> will get input from the keyboard directly, without echoing, with most windows compilers). Problem is, these function are non-standard (eg different names/headers for different compilers). But that's also true of a system() call.
The use of cin.get() can appear to be problematical (as can getchar(), part of <stdio.h>) as if there is any data left on the stream (say, for instance, a linefeed after reading in an integer value), get/getchar will return with that immediately. If you want to pause the input, use this:
cin.sync();
cin.get();
sync flushes the stream.
why not use cin.flush( );
It might work just as well (heck, it may even do exactly the same thing, for all I know), I am in the habit of using sync.
I only use system("pause") to end an external source file, so it loops back to the main().
otherwise i use getch(). system("pause") is only really good when it is not connected to main(). An example is say a function had ened ie: void I_AM_A _BEAN()
at the end of the function, since it is void you cannot use return 0 as you can't return to main if the function is decalred as void, so system pause would prompt the user to hit enter, then it would jump back to whatever it was before the function was called before.
ie: return signpost();