0

I know this question has been asked before, but nothing helped.

I have a program

void printInstructions(){
    cout << "\n******INSTRUCTIONS******\n"
         // a lot of text
         << "Press any key to begin...\t";

    /*cin.sync();
     *cin.get()
     *cin.ignore(1);
     *cout << flush;
     */

    string temp;
    cin >> temp;
    cout << '\n';

}

int main()
{

    cout << "***********************\n"
         << "******* WELCOME *******\n"
         << "***********************\n"
         << endl;

    //some stuff

    printInstructions();

   //some other stuff

}

I commented all the solutions i tried and only the one with the temp works, but with that being clumsy I'm interested why the cin.get() or cin.ignore() don't work. And yes, I tried std::flush, but it just skipped the cin. call and went onward with the program

kaya3
  • 47,440
  • 4
  • 68
  • 97
jabk
  • 1,388
  • 4
  • 25
  • 43
  • The easy fix is to change "Press any key to begin" to "Press the Enter key to begin". – Fred Larson Oct 24 '14 at 19:46
  • @FredLarson What would that change? If i use anything else then a given solution it just skips to the rest of the code – jabk Oct 24 '14 at 19:48
  • I'm not following you. What you have looks like it should work, but it won't respond to ANY key. The input is buffered, so you won't get the result of the `cin >> temp` until the user hits the Enter key. – Fred Larson Oct 24 '14 at 19:52
  • Actually, `getline` works a whole lot better than `cin >> temp`. – Fred Larson Oct 24 '14 at 19:56
  • `cin >> noskipws >> temp;` also works. – Fred Larson Oct 24 '14 at 20:03
  • Found the answer here http://cboard.cprogramming.com/cplusplus-programming/75206-cin-not-allowing-input-after-first-use-function.html – jabk Oct 27 '14 at 11:21

0 Answers0