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. Advertisements

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,456
    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,657
    Kevin Saff
    Jan 6, 2004
  3. voidstar
    Replies:
    3
    Views:
    10,553
    Mark R Rivet
    Jul 27, 2004
  4. Aleander

    cin and cin.getline()

    Aleander, Mar 6, 2005, in forum: C++
    Replies:
    5
    Views:
    9,105
    Alex Vinokur
    Mar 6, 2005
  5. erase vs. erase

    , Mar 25, 2006, in forum: C++
    Replies:
    7
    Views:
    623
    Pete Becker
    Mar 30, 2006
  6. Immortal Nephi

    How to erase stringstream buffer?

    Immortal Nephi, Feb 26, 2010, in forum: C++
    Replies:
    0
    Views:
    622
    Immortal Nephi
    Feb 26, 2010
  7. Fernando
    Replies:
    4
    Views:
    2,617
    Fernando
    Nov 16, 2011
  8. Roedy Green

    byte stream vs char stream buffer

    Roedy Green, May 7, 2014, in forum: Java
    Replies:
    20
    Views:
    729
    Silvio
    May 18, 2014
Loading...