6 Replies - 427 Views - Last Post: 22 March 2013 - 09:16 AM Rate Topic: -----

#1 clickspiker23  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 200
  • Joined: 29-October 10

infinite loop for char but not int

Posted 21 March 2013 - 07:46 PM

I have a while loop that i supposed to check if a number is in a valid range ( 0 - 5) . it does this correctly, but if say the user accidentally hit j or a letter then it just enters an infinite loop. is there a way that i can catch everything because i thoughts that was the point of a while loop, loop until correct and only correct data is entered.

here is my code.

    int columnNum;
    
    cout << "Column choices are 0 - 5" << endl;
    cout << "What column do you want to Place your piece: ";
    cin >> columnNum;
    
    
    while ((columnNum < 0) || (columnNum > 5))
    {
        cout << columnNum << " is not a valid choice please select 0 - 5" << endl;
        cout << "Column Placement: ";
        cin >> columnNum;
    }


Is This A Good Question/Topic? 0
  • +

Replies To: infinite loop for char but not int

#2 Razakel  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 5
  • Joined: 05-February 10

Re: infinite loop for char but not int

Posted 21 March 2013 - 08:28 PM

Try flushing the input after reading it in, so that the junk character (and newline) aren't there to mess with the next read-in.
Was This Post Helpful? 0
  • +
  • -

#3 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1353
  • View blog
  • Posts: 4,660
  • Joined: 19-February 09

Re: infinite loop for char but not int

Posted 21 March 2013 - 08:35 PM

Hi, columnNum is an int so, as you have found, if a character is input then the input stream goes into a fail state. While in this state the stream ignores reads.

You can test whether the stream has failed and clear the fail state.

if( cin.fail() ) {
  cout << "cin fail" << endl;
  cin.clear();
}


Was This Post Helpful? 0
  • +
  • -

#4 clickspiker23  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 200
  • Joined: 29-October 10

Re: infinite loop for char but not int

Posted 21 March 2013 - 08:48 PM

View Post#define, on 21 March 2013 - 08:35 PM, said:

Hi, columnNum is an int so, as you have found, if a character is input then the input stream goes into a fail state. While in this state the stream ignores reads.

You can test whether the stream has failed and clear the fail state.

if( cin.fail() ) {
  cout << "cin fail" << endl;
  cin.clear();
}




I actually found that page googling around and even tried something like you have. right now this is what i have but it still enters an infinite loop when something other than an integer is entered. also the book i am going through doesnt cover this yet ( as far as i can tell, or at all) so i think i am supposed to assume for this exercise that it is just checking integers. but now i want to get it to catch all errors. any advice?

    while ((columnNum < 0) || (columnNum > 5))
    {
        cout << columnNum << " is not a valid choice please select 0 - 5" << endl;
        
        // this is statement is supposed to catch anything that is not an integer. chars, strings etc
        if (cin.fail())
        {
            cin.clear();
            cout << "Column Placement: ";
            cin >> columnNum;
        }
          // otherwise its just an integer that is out of what is needed 
        else
        {
            cout << "Column Placement: ";
            cin >> columnNum;
        }
    }

Was This Post Helpful? 0
  • +
  • -

#5 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1353
  • View blog
  • Posts: 4,660
  • Joined: 19-February 09

Re: infinite loop for char but not int

Posted 21 March 2013 - 09:06 PM

You can check for fail in the while expression. You can call clear even if there is not a fail state. If the stream fails columnNum will not be changed.
Was This Post Helpful? 0
  • +
  • -

#6 DoNotWant  Icon User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 59
  • Joined: 03-November 11

Re: infinite loop for char but not int

Posted 21 March 2013 - 09:49 PM

The ASCII code for j is 116. Take a look at your while statement.
Was This Post Helpful? 0
  • +
  • -

#7 djasy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 02-February 12

Re: infinite loop for char but not int

Posted 22 March 2013 - 09:16 AM

Or i suggest you to use this fonction, it will help you to verify any type you want to cin.
bool estCorrect( unsigned int & N )
{
    while ( ! ( cin >> N ) )//tant que le type entré est différent de celui demandé
    { 
        if ( cin.eof() )
        { 
            // ^D  (^Z sous windows); Fin du flux d'entree !
            return false; 
        }
        else
        { 
        	if ( cin.fail() )
        	{
				//le \a émet un bip s'il l'entrée est incorrecte!!
	        	cout << "\aVeuillez entrer un nombre correcte S.V.P : ";
            	cin.clear();// efface les bits d'erreurs
            	cin.ignore( numeric_limits<streamsize>::max(), '\n' );// supprime la ligne erronée dans le buffer
        	}
        }
    }
    return true; //on retourne l'entrée s'il est bel est bien celui qu'on demande
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1