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 8455179
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T12:13:08+00:00 2026-06-10T12:13:08+00:00

I’ve been using the following bit of code for years to consume characters from

  • 0

I’ve been using the following bit of code for years to consume characters from cin up and including the next newline.

void skip_rest_of_line()
{
    while (cin.get() != '\n') {}
}

I would call this after a catching an exception and clearing the cin state, as in:

catch (Error& error) {
    cout << error.msg << endl;
    cin.clear();
    skip_rest_of_line();
    }

The next read from cin skips the remaining newline as whitespace.

I’ve just changed to Xcode 4.4.1 under Lion. Using LLVM GCC 4.2 GNU++98, libstdc++(GNU C++ standard library), it works like it always did.

But using Apple LLVM Compiler 4.0, c++11, libc++ (LLVM C++ standard library with C++11 support) it looks like there are multiple problems with recognizing the newline. The same code as above requires a second return keystroke to satisfy the loop! The same problem appears with the alternative:

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

Finally, using getchar instead of cin.get() in the while loop works like it always did!

Is this a bug in libc++? Or have I missed something either in Xcode or C++11?

In response to Howard Hinnant, here is a little test case that shows what happens without any exceptions, etc. in the picture:

#include <iostream>
#include <vector>
using namespace std;

int main()
{
 for(int count = 0; count < 2; count++) {
    cout << "ready for input: " << endl;
    int i;
    vector<int> v;
    while(cin >> i) {
        v.push_back(i);
        if(i == 3) {
            while(cin.get() != '\n') {}
            break;
            }
        }
    for(auto it = v.begin(); it != v.end();  ++it)
        cout << *it << ' ';
    cout << endl;
}

cout << "done" << endl;
return 0;

}

It reads and saves ints until it’s saved a 3, whereupon it reads and discards through the next newline, and then repeats the whole thing, so you can see that the stuff after the 3 gets discarded from the input stream. With Xcode 4.4.1 Lion with GNU++98 || GNU++11, libstd++, I get what I expect when I put in these two lines each terminated by a space followed by a RETURN keystroke:

1 2 3 4 5 
6 7 8 3 

and I get in the output window of Xcode:

ready for input: 
1 2 3 4 5 
1 2 3 
ready for input: 
6 7 8 3 
6 7 8 3 
done

But with C++11, libc++, nothing happens with the first line until I enter a SECOND RETURN keystroke, after both lines of input, but the proper discarding has still happened. So the extra space below after each input line is what the second keystroke looks like.

ready for input: 
1 2 3 4 5 

1 2 3 
ready for input: 
6 7 8 3 

6 7 8 3 
done

The very same behavior happens with using the above cin.ignore call instead of the while loop around cin.get(). So the problem is “Why is the second RETURN keystroke needed?”

  • 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-10T12:13:09+00:00Added an answer on June 10, 2026 at 12:13 pm

    I think this is a bug in libc++ on Lion, and fixed on Mountain Lion. But it is difficult to be certain without a complete test case.

    Update

    Thanks for the test case. Confirmed bug on Lion and fixed on Mountain Lion.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I need to clean up various Word 'smart' characters in user input, including but
I have a text area in my form which accepts all possible characters from
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I have a jquery bug and I've been looking for hours now, I can't
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text

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.