Thread: help with cin.fail()

  1. #1
    Un Artiste Extraordinaire volk's Avatar
    Join Date
    Dec 2002
    Posts
    357

    help with cin.fail()

    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	int  first;
    	char dash;
    	int  last;
        char choice;
    	
    	for ( ; ; )
    	{
    		system("cls");
    
    	cout << "Enter a range of numbers\n"
    		 << "(ex: 1-10)" << endl;
    
    	cin  >> first >> dash >> last;
    
    	while (dash != '-')
    	{
    		cout << "Invalid range" << endl;
    		cin  >> first >> dash   >> last;
    	}
    
    	while (cin.fail())
    	{
    		cout << "Invalid range" << endl;
    		cin  >> first >> dash   >> last;
    	}
    		
    
    	for (int i = first; i <= last; i++)
    		
    		cout << i << endl;
    
    	cout << "\nWould you like to enter another range?\n"
    		 << "(Y/N)" << endl;
    	
    	cin  >> choice;
    
    	if (toupper(choice) == 'N')
    		exit(EXIT_SUCCESS);
    
    	}
    
    	return 0;
    }
    Why does the program go crazy when an invalid range is entered?

    It's suppose to just say "Invalid range" and ask the user to re-enter another range, but it displays "Invalid range" infinitely and never reaches the cin statement.

    Code:
    while (cin.fail())
    	{
    		cout << "Invalid range" << endl;
    		cin  >> first >> dash   >> last;
    	}

  2. #2
    Un Artiste Extraordinaire volk's Avatar
    Join Date
    Dec 2002
    Posts
    357
    Is there something else I can use besides cin.fail?

  3. #3
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    try this small addendum...
    Code:
    while (cin.fail())
    	{
                                    cin.clear();
    		cout << "Invalid range" << endl;
    		cin  >> first >> dash   >> last;
    	}
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  4. #4
    Un Artiste Extraordinaire volk's Avatar
    Join Date
    Dec 2002
    Posts
    357

    Unhappy

    Originally posted by Stoned_Coder
    try this small addendum...
    Code:
    while (cin.fail())
    	{
                                    cin.clear();
    		cout << "Invalid range" << endl;
    		cin  >> first >> dash   >> last;
    	}
    That code didn't work.

  5. #5
    Open to suggestions Brighteyes's Avatar
    Join Date
    Mar 2003
    Posts
    204
    That code didn't work.
    That's because you cleared the error flags, but didn't get rid of the bad input. If cin fails it leaves the input that it failed on in the stream, so if you don't clear it out you'll read it next time and fail again ad infinitum. Try this
    Code:
    while (cin.fail())
    {
        cin.clear();
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
        cout << "Invalid range" << endl;
        cin  >> first >> dash   >> last;
    }
    And be sure to include <limits>, that's where numeric_limits lives.

  6. #6
    Un Artiste Extraordinaire volk's Avatar
    Join Date
    Dec 2002
    Posts
    357
    Originally posted by Brighteyes

    Code:
    cin.ignore(numeric_limits<streamsize>::max(), '\n');
    Can you please explain in-depth what that does?


    And be sure to include <limits>, that's where numeric_limits lives.


    Doesn't uisng namespace std; include that?

  7. #7
    Open to suggestions Brighteyes's Avatar
    Join Date
    Mar 2003
    Posts
    204
    Can you please explain in-depth what that does?
    The ignore function of cin reads and discards from the input stream n characters, or until it gets to a stopping character. The declaration of ignore is
    Code:
    basic_istream<charT,traits>& ignore(streamsize n = 1, int_type delim = traits::eof());
    It's kinda complicated, but you should get the idea. Anyway, numeric_limits is a template that gets a bunch of stuff from the type you give it like the maximum and minimum values. I used numeric_limits on the streamsize type and called the max function to get the maximum size. So ignore will read that many characters from the stream and discard them unless it hits the stopping character. I set the stopping character to a newline. The call basically says "Read the maximum number of characters the stream can hold, or until you get to a newline and throw away what you find".
    Doesn't uisng namespace std; include that?
    Nope, the using directive only makes names in that namespace available in the global scope so that you don't have to prefix everything with std::, it doesn't include them if you forget the header that they're declared in. Just like you can't use cin and cout if you don't include iostream.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. cin.fail() function
    By robid1 in forum C++ Programming
    Replies: 1
    Last Post: 04-26-2004, 01:04 PM
  2. cin.fail
    By bj31t in forum C++ Programming
    Replies: 3
    Last Post: 04-01-2004, 03:26 PM
  3. cin.fail() trouble :(
    By volk in forum C++ Programming
    Replies: 9
    Last Post: 07-03-2003, 06:25 PM