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

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.!!"

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.