cin.fail() trouble :(

This is a discussion on cin.fail() trouble :( within the C++ Programming forums, part of the General Programming Boards category; Code: #include < iostream > using namespace std; int main() { int num; int num2; int i; int j; cout ...

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

    cin.fail() trouble :(

    Code:
    #include < iostream >
    using namespace std;
    
    int main()
    {
    	int num;
    	int num2;
    
    	int i;
    	int j;
    
    	cout << "Enter a starting number" << endl;
    	cin  >> num;
    
    	cout << "\nEnter an end number" << endl;
    	cin  >> num2;
    
    	while ( cin.fail() || num % 2 != num2 % 2 )
    	{
    		cin.clear  ();
    		cin.ignore ( INT_MAX, '\n' );
    
    		cerr << "\nError (NO MIDDLE NUMBER FOUND)...re-enter..." << endl;
    
    		cerr << "\nStarting number: ";
    		cin  >> num;
    		
    		cerr << "\nEnd number: ";
    		cin  >> num2;
    	}
    
    	if ( num > num2 )
    	{
    		for ( i = num2, j = num; j != i; i++, j-- ) 
    		{
    			;
    		}
    	}
    
    	else if ( num == num2 )
    	{
    		cout << "\nBoth numbers are the same" << endl;
    		exit ( EXIT_SUCCESS );
    	}
    	
    	else 
    	{
    		for ( i = num, j = num2; j != i; i++, j-- ) 
    		{
    			;
    		}
    	}
    
    	cout << "\nThe number between the numbers you entered is " << i << endl;
    
    	return 0;
    }
    If a letter is entered for the starting number, an error message is displayed, but before the error message is displayed the cout statement for the end number is executed.

    How can I prevent that cout statement from executing before the error message? Why does that cout statement even execute in the first place?

    I came up with the solution below, but it's too lenghty, and it uses a goto statement.

    Code:
    #include < iostream >
    
    using std::cout;
    using std::cin;
    using std::cerr;
    using std::endl;
    
    int main()
    {
    	int num;
    	int num2;
    
    	int i;
    	int j;
    
        START://GOTO LABEL
    		
    		cout << "Enter a starting number" << endl;
    		cin  >> num;
    
    	while ( cin.fail() )
    	{
    		cin.clear  ();
    		cin.ignore ( INT_MAX, '\n' );
    
    		cerr << "\nError...re-enter..." << endl;
    
    		cerr << "\nStarting number: ";
    		cin  >> num;
    	}
    
    	cout << "\nEnter an end number" << endl;
    	cin  >> num2;
    
    	while ( cin.fail() )
    	{
    		cin.clear  ();
    		cin.ignore ( INT_MAX, '\n' );
    
    		cerr << "\nError...re-enter..." << endl;
    
    		cerr << "\nEnd number: ";
    		cin  >> num2;
    	}
    	
    	if ( num % 2 != num2 % 2 )
    	{
    		cerr << "\nError (NO MIDDLE NUMBER FOUND)...re-enter..." << endl;
    
    		system ( "pause" );
    
    		system ( "cls" );
    
    		goto START;
    	}
    
    	if ( num > num2 )
    	{
    		for ( i = num2, j = num; j != i; i++, j-- ) 
    		{
    			;
    		}
    	}
    	
    	else if ( num == num2 )
    	{
    		cout << "\nBoth numbers are the same" << endl;
    		exit ( EXIT_SUCCESS );
    	}
    
    	else
    	{
    		for ( i = num, j = num2; j != i; i++, j-- ) 
    		{
    			;
    		}
    	}
    
    	cout << "\nThe number between the numbers you entered is " << i << endl;
    
    	return 0;
    }
    Last edited by volk; 07-03-2003 at 05:02 PM.

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Create yourself a function to get a number from the user, and then simply call it twice to get both numbers.

    You can base your code on one of the entries in the FAQ.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Un Artiste Extraordinaire volk's Avatar
    Join Date
    Dec 2002
    Posts
    357
    Originally posted by Hammer
    Create yourself a function to get a number from the user, and then simply call it twice to get both numbers.

    You can base your code on one of the entries in the FAQ.


    Anyway, this is the best I could come up with:

    Code:
    #include < iostream >
    
    using std::cout;
    using std::cerr;
    using std::cin;
    using std::endl;
    
    int main()
    {
    	int num;
    	int num2;
    
    	int i;
    	int j;
    
    	cout << "Enter a starting number" << endl;
    	cin  >> num;
    
    	cout << "\nEnter an end number" << endl;
    	cin  >> num2;
    
    	while ( cin.fail() || num % 2 != num2 % 2 )
    	{
    		cin.clear  ();
    		cin.ignore ( INT_MAX, '\n' );
    
    		system ( "cls" );
    
    		cerr << "\nError (NO MIDDLE NUMBER FOUND)...re-enter..." << endl;
    
    		cerr << "\nStarting number: ";
    		cin  >> num;
    		
    		cerr << "\nEnd number: ";
    		cin  >> num2;
    	}
    
    	if ( num > num2 )
    	{
    		for ( i = num2, j = num; j != i; i++, j-- ) 
    		{
    			;
    		}
    	}
    
    	else if ( num == num2 )
    	{
    		cout << "\nBoth numbers are the same" << endl;
    		exit ( EXIT_SUCCESS );
    	}
    	
    	else 
    	{
    		for ( i = num, j = num2; j != i; i++, j-- ) 
    		{
    			;
    		}
    	}
    
    	cout << "\nThe number between the numbers you entered is " << i << endl;
    
    	return 0;
    }
    I still don't know what makes that darn cout statement execute, though.

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    What about an adaption of this:
    Code:
    #include <iostream>
    using namespace std;
    
    int GetNumber(void)
    {
      int i;
      while (cout <<"Enter your number: " <<flush
             && !(cin >> i))
      {
        cin.clear();
        cin.ignore (INT_MAX, '\n');
      }
      return i;
    }
    
    int main()
    {
      int i = GetNumber();
      cout <<"Your entered " <<i <<endl;
      return 0;
    }
    In your code, after you enter a letter for the first number, you must clear the buffer straight away, before doing another cin.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by volk

    I still don't know what makes that darn cout statement execute, though.
    Just a point here, for you and everyone else posting problems. The above statement is way to vague. Which cout are you talking about here? You have like five or more. Most people, well me anyway, don't just copy / paste / compile every code snippet people see here to try and fix your problem.

    What I do is fairly linear:
    1) Look at the code itself for obvious syntax issues.
    2) Look at the code for other obvious errors (such as wandering off the end of your array etc).

    Very rarely will I actually bother compiling something.

    As such, since I assume most people "troubleshoot" your code the same way, you should be saying something like:
    I am getting {this specific issue} when I get to the hilighted code:
    Code:
    line of code
    another line
    line /* point 1 */
    line of code
    line of code
    I expected {this specific result}, and am not getting that.
    Something to that effect. Just saying "Why do I get the cout statement?" doesn't really help anyone help you. This post isn't to specificly pick on you, so don't take it that way. Rather it is to everyone who ever plans on asking for help.

    If you want help, you need to be as specific as possible in every detail.

    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Un Artiste Extraordinaire volk's Avatar
    Join Date
    Dec 2002
    Posts
    357
    My solution uses less lines than Hammer's, no?

  7. #7
    Un Artiste Extraordinaire volk's Avatar
    Join Date
    Dec 2002
    Posts
    357
    As for you, quzah:

    "If a letter is entered for the starting number, an error message is displayed, but before the error message is displayed the cout statement for the end number is executed."

    cout << "\nEnter an end number" << endl;



    Also, don't you belong in the C forum. What are you doing here?
    Last edited by volk; 07-03-2003 at 05:59 PM.

  8. #8
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by volk
    My solution uses less lines than Hammer's, no?
    No, plus mine works...
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  9. #9
    Un Artiste Extraordinaire volk's Avatar
    Join Date
    Dec 2002
    Posts
    357
    Originally posted by Hammer
    No, plus mine works...
    Are you implying that mine doesn't work?

  10. #10
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Only that yours does a read on a stream that is broken (assuming the user enters a letter instead of a number). And your non-standard method of recovery isn't ideal:
    >>system ( "cls" );

    So yeah, yours works, but it isn't a clean solution.

    The output from mine is simply:
    Code:
    Enter your number: a
    Enter your number: b
    Enter your number: c
    Enter your number: 11
    You entered 11
    Of course, it's personal preference...
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Trouble with assignment in C
    By mohanlon in forum C Programming
    Replies: 17
    Last Post: 06-23-2009, 10:44 AM
  2. Replies: 6
    Last Post: 01-03-2007, 02:02 PM
  3. Is it so trouble?
    By Yumin in forum Tech Board
    Replies: 4
    Last Post: 01-30-2006, 03:10 PM
  4. trouble scanning in... and link listing
    By panfilero in forum C Programming
    Replies: 14
    Last Post: 11-21-2005, 11:58 AM
  5. C++ program trouble
    By senrab in forum C++ Programming
    Replies: 7
    Last Post: 04-29-2003, 11:55 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21