Hi guys how are you , hope you are ok. I have a small problem with my code. briefly my program is about chained sequence numbers which requires a user to enter sequences separated by (-1) in an input file as follow:
123 122 121
-1
45 67 89
-1

in the output file the following will be shown:
the sequence 123 122 121 is chained
the sequence 45 67 89 is not chained

( a sequence is called chained when a number is differs by one digit from the previous number )
As can be seen int input that i have to enter -1 between each sequence... that's ok. But my main problem is that i have to enter -1 also after the last entered sequence otherwise an error will be shown in the compiler. so i want to write the input like that :
123 121 123
-1
123 455 788

insted of
123 121 123
-1
123 455 788
-1

also my code should accept only positive numbers, thats ok but in my code -1 will be considred and i think that the first problem will sove this problem

Therefore, how can i fix that problem... i tried too much times but i didn't reach to any thing.... i wonder if you could help me guys.
The code is :

#include <iostream>
#include <fstream>
using namespace std;
   bool differsByOneDigit ( int , int );
   void outputResults ( ostream & , const int * , int , bool );

int main()
{
   
   ifstream input ( "c:\\data.txt" );
   ofstream output ( "c:\\out.txt" );
 
   if ( input.fail() || output.fail() )
   {
       cout <<"input or output file did not open!!! " << endl;
       return 1;
   }
   int first  , next ; //32bit int == 2147483647
   int sequence [ 10 ];//sequence read
   int seqLength = 0; //sequence length
   int j;
   int temp;
   bool match; //matching sequence
   int firstSeq;

   if ( ! ( input >> temp ) )
   {
       //until you here otherwise, bad file format will be handled by program termination.
       cout << "File has a non-int within" << endl;
       cout << "Bad file format , terminating program!!! "<< endl ;
       return 1;
   }
   else if ( (temp > 1000000000) || (temp < -1 ) )
   {
       //until you here otherwise, out of range number  will be handled by program termination
       cout  << "File has out of range number "<< endl;
       cout  << " TERMINATING program!!! "<< endl;
       return 1;
   }
   while ( ! input.eof()  )
   {
       firstSeq = temp;
       //read next sequence
       while ( temp != -1 && seqLength < 10  && !input.eof()  )
       {
           sequence [ seqLength ++ ] = temp;
           if ( ! ( input >> temp ) )
           {
               //until you here otherwise, bad file format will be handled by program termination.
               cout  << "File has a non-int within"<< endl;
               cout << "Bad file format , terminating program!!! "<< endl ;
               return 1;
           }
           else if ( (temp > 1000000000) || (temp < -1 ) ) 
           {
               //until you here otherwise, out of range number will be handled by program termination
               cout  << "File has out of range number "<< endl;
               cout << " TERMINATING program!!! " << endl;
               return 1;
           }
       }
       if ( temp != -1 )
       {
           //until you here otherwise, bad file format will be handled by program termination.
           cout  << "File has a sequence length greater than 10!!!"<< endl;
           cout  << "Bad file format , terminating program!!! "<< endl;
           return 1;
       }
       if ( seqLength <= 1 )
       {
       
     cout << "Sequence of one or less found!!"<< endl ;
     cout << "Bad file format , terminating program!!! " << endl;
           return 0;
    
       }
       sequence [ seqLength ] = -1;
       j = 0;
       first = sequence [ j ];
       next = sequence [ j + 1 ];
       j += 2;
       match = true;
       while ( match && next != -1)
       {
           match = differsByOneDigit( first , next );
           first = next;
           next = sequence [ j ];
           j++;
       }
       if ( match )
           match = differsByOneDigit ( first , firstSeq ); //check front to back
       outputResults ( output , sequence , seqLength , match );
       if ( ! ( input >> temp ) )
       {
           if ( !input.eof() )
           {
               //until you here otherwise, bad file format will be handled by program termination.
               cout << "File has a non-int within"<< endl ;
               cout  << "Bad file format , terminating program!!! "<< endl;
               return 1;
           }
       }
       else if ( (temp > 1000000000) || (temp < -1 ) ) 
       {
           //until you here otherwise, out of range number will be handled by program termination
           cout << "File has out of range number "<< endl ;
           cout  << "TERMINATING program!!! "<< endl;
           return 1;
       }
       seqLength = 0;
   }
   output.close();
   input.close();
   return 0;
}
bool differsByOneDigit ( int first , int next )
{
   int differentDigits = 0;
   while ( first != 0 && next != 0 && differentDigits <= 1 )
   {
       if ( first % 10 !=  next % 10 ) //count different digits
           differentDigits ++;
       first /= 10;
       next /= 10;
   }
   if ( differentDigits > 1 || first || next )
       return false;
   else
       return true;
}
void outputResults ( ostream & output , const int * sequence  , int length , bool isChainedSequence )
{
   int j = 0;
   output << "The sequence : ";
   cout  << "The sequence : ";
   while ( j < length && sequence [ j ] != -1 )
   {
       output << sequence [ j ] << " " ;
       cout << sequence [ j ++ ] << " " ;
       if ( j % 7 == 0 )
       {
           output << endl;
           cout << endl;
       }
   }
   if ( isChainedSequence )
   {
       output << " is chained. " << endl;
       cout << endl << "has been found....OPEN the output file to CHECK whether is it chained or not" << endl;
   }
   else
   {
       output << " is not chained. " << endl;
       cout << endl << "has been found....OPEN the output file to CHECK whether is it chained or not" << endl;
   }
   output << endl <<"****************************************************" << endl;
   cout << endl <<"****************************************************" << endl;
}

REGARDS

Member Avatar for iamthwee

You're way over complicating things kiddo.

1. read in a line, if it isn't -1 do your work otherwise skip to the next line. And your method to find out if a sequence is chained could be greatly simplified.

Member Avatar for iamthwee

Look here

#include <iostream>

using namespace std;

int main()
{
    int array[] = {121,122,121,122,123};
    
    int size = 5;
    
    int count = 0;
    int tmp, tmpTwo;
    for ( int i = 0; i < size; i++ )
    {
        tmp = array[i];
        tmpTwo = array[i+1];
        if (( tmp - tmpTwo == 1 )||( tmp - tmpTwo == -1 ))
        {
             count++; //increment the counter    
        }
    }
    
    if (count == size - 1 )
    {
      cout << "Your sequence is chained";
    }
    else 
      cout << "Your sequence aint chained!";
    
    cin.get();
}
while ( ! input.eof()  )
   {
    ...

There's a difference between C++'s input/output and other languages like pascal etc. Instead try checking the return value of the input routine.

while( input )
{
 ...
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.