How to erase cin's buffer stream?

Discussion in 'C++' started by Immortal Nephi, Apr 14, 2010.

  1. The message in the console screen shows: press any key in prompt.
    The string can only accept one character at this time. If key is not
    valid, then message shows invalid prompt and try again. The problem
    is that loop does not let you to type again and continues to copy one
    character into string. The string will have two or more characters.
    How do I clear or erase cin’s buffer stream?

    Int main()
    {
    cout << “Press any key.\n”
    << “Prompt: “;

    string Prompt;

    cin.width( 1 );
    ws( cin );
    cin >> Prompt;

    while( ( Prompt != “A” && Prompt != “B” ) )
    {
    cout << “Invalid prompt. Try again.\n\n”;
    cout << “Press any key.\n”
    << “Prompt: “;

    ws( cin );
    cin >> Prompt;
    }

    cout << “Done.” << endl;

    return 0;
    }
    Immortal Nephi, Apr 14, 2010
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. TaiwanNoWhere

    cin and cin.clear() problem

    TaiwanNoWhere, Oct 13, 2003, in forum: C++
    Replies:
    8
    Views:
    4,102
    P.J. Plauger
    Oct 17, 2003
  2. Chris Mantoulidis

    std::cin.ignore() and std::cin.clear()

    Chris Mantoulidis, Jan 6, 2004, in forum: C++
    Replies:
    5
    Views:
    17,163
    Kevin Saff
    Jan 6, 2004
  3. Aleander

    cin and cin.getline()

    Aleander, Mar 6, 2005, in forum: C++
    Replies:
    5
    Views:
    8,689
    Alex Vinokur
    Mar 6, 2005
  4. erase vs. erase

    , Mar 25, 2006, in forum: C++
    Replies:
    7
    Views:
    363
    Pete Becker
    Mar 30, 2006
  5. Fernando
    Replies:
    4
    Views:
    1,677
    Fernando
    Nov 16, 2011
Loading...

Share This Page