dont use this. it is about as overkill as you can possibly go...Originally Posted by joeyzt
try: cin.get();
dont use this. it is about as overkill as you can possibly go...Originally Posted by joeyzt
try: cin.get();
I like to use getline(), since it cleans out the keyboard input buffer, including the '\n' (for the benefit of subsequent >>cin or getline() operations.
Something like:
Code:#include <iostream> int main() { void wait_for_enter(void); char name[BUFSIZ]; std::cout << "Let's play a game: " << std::endl << std::endl; std::cout << "Press \"Enter\" when you are ready: "; wait_for_enter(); std::cout << std::endl << " OK! Ready to begin" << std::endl; std::cout << std::endl << " Please enter your name: "; std::cin.getline(name, sizeof(name)); std::cout << std::endl << " Hi, " << name << std::endl; // etc. // etc. return 0; } void wait_for_enter(void) { char ch[BUFSIZ]; std::cin.getline(ch, sizeof(ch)); }
whups i meant cin.get();
not cin.getline();
but for whatever reason my program does not wait for the user even with cin.get() or cin.getline().
eg.
when this code is executed the program will printCode:cout << "press enter to begin" << endl; cin.get(); cout << "blah blah blah";
press enter to begin
blah blah blah
without waiting for a carriage return input from the user after the first line is printed.
>>for whatever reason my program does not wait for the user even with cin.get() or cin.getline().
Prior calls to cin using the >> operator will likely leave a newline in the input stream. This will cause both get and getline terminate immediately without waiting for input as you would expect. If you have no qualms against flushing the input stream then this will work just fine for you.
Do not forget to include <limits> as it holds numeric_limits.Code:std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
A better solution of course is to eschew formatted input in favor of line input and a manual conversion.
that did the trick!
thank you all.