Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8757731
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T14:22:25+00:00 2026-06-13T14:22:25+00:00

Possible Duplicate: Why is this cin reading jammed? I overloaded the istream operator (

  • 0

Possible Duplicate:
Why is this cin reading jammed?

I overloaded the istream operator (istream &operator>>...) and it takes in a Point of the format:

(<x-coordinate>,<y-coordinate>)

I want to test this out multiple (10) times, and so have written:

for (int i = 0; i < 10; i++)  {
    cin >> a;
    if (!cin.fail()) { cout << a << endl; }
    else { cout << "Invalid input!" << endl; cin.clear(); }
}

EDIT:

I now have the following code:

for (int i = 0; i < 10; i++)  {
    cin >> a;
    if (!cin.fail()) { cout << a << endl; }
    else {
        cout << "Invalid input!" << endl; cin.clear();
        while (!cin.eof()) { cin.ignore(); } cin.ignore();
    }
}

The ignore was suggested by Cthulhu. However, the problem is cin still outputs “Invalid input!” after running through the code above:

(3,3) <-- input
(3,3) <-- output
Invalid output! <-- second output

Is there a way that I can clear what is in cin?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T14:22:26+00:00Added an answer on June 13, 2026 at 2:22 pm

    cin.clear() does not empty the buffer, it resets error flags on the stream.
    You then have to call cin.ignore

    istream&  ignore ( streamsize n = 1, int delim = EOF );
    

    Extracts characters from the input sequence and discards them.

    The extraction ends when n characters have been extracted and
    discarded or when the character delim is found, whichever comes first.
    In the latter case, the delim character itself is also extracted.


    cin.clear();
    cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    

    Numeric Limits

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Why is this code invalid in C#? Conditional operator cannot cast implicitly?
Possible Duplicate: Priority queue in .Net This question is similar, but i want to
Possible Duplicate: C++ cin whitespace question I'm having problem trying to understand this piece
Possible Duplicate: How to conduct buffer overflow in PHP/Python? I was reading this tutorial
Possible Duplicate: Java this.method() vs method() I've been reading some things and doing some
Possible Duplicate: Is this a good way to generate a string of random characters?
Possible Duplicate: How does this CSS triangle shape work? Please help me i need
Possible Duplicate: Why do this Ruby object have two to_s and inspect methods that
Possible Duplicate: Why does this integer division yield 0? I have a C header
Possible Duplicate: Why does this intentionally incorrect use of strcpy not fail horribly? Below

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.