0

Hi All

Please help me with the following code, it works fine when i enter
1234567890 but goes mad when i enter one more digit i.e 12345678901.
It keeps on looping and displays infinitely:-

"Invalid Input. Please try again.!!"

How can i fix/correct this?

Any help will be appreciated!

#include <iostream>
#include <cassert>
using namespace std;

int main( )
{
   int aDigit[ 5 ]; 
   
   cout << "Enter 5 digits, each on a new line." << endl;

   cout << "Your digits must be between 0 and 9: " << endl;
   
   for ( int i = 0; i < 5; )
   {
     cin >> aDigit[ i ];

     if((aDigit[i] >= 0) && (aDigit[i] <= 9))
     {
       cout << "Thank you. That is accepted as number " << i+1 <<"." <<  endl;      
       i++;
     }
     else
     {
       cout << "Invalid Input. Please try again.!!" << endl;
	   
     }
      
   }

   cout << endl << "Digits in reverse order:" << endl;

   for ( int j = 4; j >= 0; j-- )
   {
      cout << aDigit[ j ] << endl;
   }

   return 0;
}
2
Contributors
1
Reply
2
Views
9 Years
Discussion Span
Last Post by Sci@phy
0

int has it's largest number, and you can't insert a larger number in it than that.
But I don't think that's even point. Your code asks you to input each digit in NEW LINE. Inputing them on same line will produce digit larger that nine, and thus output: "Invalid Input. Please try again.!!"

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.