Thread: Why it can't read in a number?

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    32

    Why it can't read in a number?

    Hi, I can't figure out why it can't read in the number N... Can you please help?

    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;
    }

  2. #2
    Registered User
    Join Date
    Jan 2009
    Posts
    10
    cin.clear()

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    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.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Registered User
    Join Date
    Dec 2009
    Posts
    32
    Quote Originally Posted by dwks View Post
    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';
    }

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    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.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Looking for constructive criticism
    By wd_kendrick in forum C Programming
    Replies: 16
    Last Post: 05-28-2008, 09:42 AM
  2. Read the number of characters
    By rehan in forum C++ Programming
    Replies: 5
    Last Post: 06-27-2007, 10:57 AM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. How can I read one number at a time?
    By Bad_Scooter in forum C++ Programming
    Replies: 2
    Last Post: 07-18-2003, 09:58 PM
  5. how to read in a negative number using cin
    By revelation437 in forum C++ Programming
    Replies: 4
    Last Post: 04-02-2003, 02:32 PM