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

  • Home
  • SEARCH
  • 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 6361351
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T23:45:59+00:00 2026-05-24T23:45:59+00:00

I am just working through the book Accelerated C++. (My problem is located on

  • 0

I am just working through the book Accelerated C++. (My problem is located on page 57, if you guys have the book with you)

The problem is the following:
We do have a function which reads student grades:

...
while (in >> x) {    // in and x are defined as an istream& and as a double
    hw.push_back(x);    // hw is vector defined as vector<double>&
}
in.clear();
...

Now, in book and also on the cplusplus.com refernce is stated that the clear function resets all the error states and that the input is now again ready to read some input.
The problem is that if I put a:

int a = 0;
cin >> a;
cout << a << endl;

after the function it jumps the cin and just gives me a 0. Did I understand the function of cin.clear() totally wrong or what can I do to get the cin active again.

As I had the same problem a while before I read the book, I know that I solved the problem back then with the following line:

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

Of course I then have to hit an extra enter key but it eats all the stuff which comes before and which makes the cin not to work.

The thing is that neither .clear nor .ignore work properly alone but using them both together I am able to enter something for the variable a;

EDIT: Ok, here is the whole code. This is something I have written myself, which is not from the book.

istream& in = cin; 
int var = 0;
vector<int> vec;
while ( in >> var ) { 
    vec.push_back(var); 
}
for (int f = 0; f < vec.size(); ++f) {
    cout << vec[f] << endl;
}
cin.clear();
cout << "cleared" << endl;
    int a = 0;
    cin >> a;
    cout << a << endl;
  • 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-05-24T23:46:00+00:00Added an answer on May 24, 2026 at 11:46 pm

    The call to clear clears the error state set by a failed read. It doesn’t do anything with the characters that might be present in the input stream.

    If the error state is a result of failing to read a double, it is likely that the next character will also fail as an integer.

    If you try

    char ch;
    cin >> ch;
    

    I’m sure that will work better.

    Otherwise you will have to ignore some characters to get rid of the unreadable input.

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

Sidebar

Related Questions

I'm currently working my way through Accelerated C++ and just came across this in
I've just started working through C++ Primer Plus and I have hit a little
I'm new to cakephp so have just been working through the cookbook and am
I am new to cakephp and have been working through the Apress book Beginning
Just working through the Agile Web Development with Rails book and near the closing
I'm working through the O'Reilly Programming Scala book, and have run into a stumbling
When working through MathLink with slave kernel I have a problem with correct parsing
I was just working through a iPhone book and I noticed a comment that
I have just started self-studying the Dragon book of Compiler Design. I am working
Hey guys, im working through the Introduction to Programming in Java book and one

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.