# Why it can't read in a number?

• 12-24-2009
pantera
Why it can't read in a number?

Code:

```// Exercise 8 - Chapter 5 ("Programming: Principles and Practice using C++") #include "std_lib_facilities.h" class out_of_range_error{}; int main() try {         vector <int> numbers;         int num, N, sum;                 cout << "Enter some numbers (press '|' at prompt to stop):";         while (cin>>num)                 numbers.push_back(num);         cout << "Enter how many of the first numbers you want to sum: \n";         cin >> N; // PROBLEM HERE!!! - It cannot read in N!                cout << "The sum of the first " << N << " numbers: ";         sum = 0;         for (int i=0; i<N; ++i) {                 sum += numbers[i];                 cout << numbers[i] << ", ";                 if (i==(N-1)) cout << " and";         }         cout << " is " << sum << endl; } catch (out_of_range_error) {         cout << "Range error!\n";         return 1; } catch (...) {         cerr << "Exception: Something went wrong\n";         return 2; }```
• 12-25-2009
Viewer
cin.clear()
• 12-26-2009
dwks
When you try to parse a number out of '|', that character will remain in the input stream, and the stream (cin) will enter an error state. You need to remove the '|' from the stream and clear the error state as well, before you can use it for anything else (for example, reading another number).

cin.clear() will clear the error state; but that leaves the offending '|' still in the input stream. std::cin.ignore() and std::cin.clear()
To deal with both problems, you might consider using
Code:

```cin.ignore(1000, '\n');  // ignore lots of characters, up until a newline cin.clear();  // clear error state```
There's a better constant to use than 1000, of course. I leave it up to you to search the boards and find it if you're curious. :)
• 12-28-2009
pantera
Quote:

Originally Posted by dwks
When you try to parse a number out of '|', that character will remain in the input stream, and the stream (cin) will enter an error state. You need to remove the '|' from the stream and clear the error state as well, before you can use it for anything else (for example, reading another number).

cin.clear() will clear the error state; but that leaves the offending '|' still in the input stream. std::cin.ignore() and std::cin.clear()
To deal with both problems, you might consider using
Code:

```cin.ignore(1000, '\n');  // ignore lots of characters, up until a newline cin.clear();  // clear error state```
There's a better constant to use than 1000, of course. I leave it up to you to search the boards and find it if you're curious. :)

Thank you for the hint. Does this mean I need to add these two statements as in the following? It still doesn't work somehow :-<

Code:

```// Exercise 8 - Chapter 5 ("Programming: Principles and Practice using C++") #include "std_lib_facilities.h" int main() try {         vector <int> numbers;         int num, sum;         unsigned int N = -1;                 cout << "Enter some numbers (press '|' at prompt to stop):";         while (cin>>num)                 numbers.push_back(num);                 cin.ignore(1000, '\n');  // ignore lots of characters, up until a newline         cin.clear();  // clear error state                cout << "Enter how many of the first numbers you want to sum: \n";         cin >> N; // PROBLEM HERE!!! - It cannot read in N!                if (N>numbers.size()) error("Too many numbers than are available in the series", numbers.size());         if (N<1) error("The number must be at least 1");         cout << "The sum of the first " << N << " numbers: ";         sum = 0;         for (unsigned int i=0; i<N; ++i) {                 sum += numbers[i];                 cout << numbers[i] << ", ";                 if (i==(N-1)) cout << " and";         }         cout << " is " << sum << endl; } catch (runtime_error e) {        // this code is to produce error messages         cout << e.what() << '\n'; }```
• 12-29-2009
dwks
Yes, well, it seems to work if you switch the order of the lines I supplied, and instead use
Code:

```cin.clear();  // clear error state cin.ignore(1000, '\n');  // ignore lots of characters, up until a newline```
Sorry about that. I didn't know that ignore() does not work on streams that are in a fail state.