DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 2 of 2
  1. #1
    Join Date
    Dec 2006
    Posts
    5

    Unhappy A small problem in the output

    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. 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 :

    Code:
    #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 << endl << "File has a non-int within";
           cout << endl << "Bad file format , terminating program!!! ";
           return 1;
       }
       else if ( temp > 1000000000 )
       {
           //until you here otherwise, number greater than 1 billion will be handled by program termination
           cout << endl << "File has number greater than 1 billion";
           cout << endl << "Too large number , terminating program!!! ";
           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 << endl << "File has a non-int within";
                   cout << endl << "Bad file format , terminating program!!! ";
                   return 1;
               }
               else if ( temp > 1000000000 ) 
               {
                   //until you here otherwise, number greater than 1 billion will be handled by program termination
                   cout << endl << "File has number greater than 1 billion";
                   cout << endl << "Too large number , terminating program!!! ";
                   return 1;
               }
           }
           if ( temp != -1 )
           {
               //until you here otherwise, bad file format will be handled by program termination.
               cout << endl << "File has a sequence length greater than 10!!!";
               cout << endl << "Bad file format , terminating program!!! ";
               return 1;
           }
           if ( seqLength <= 1 )
           {
               cout << endl << "Sequence of one or less found!!";
               continue;
           }
           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 << endl << "File has a non-int within";
                   cout << endl << "Bad file format , terminating program!!! ";
                   return 1;
               }
           }
           else if ( temp > 1000000000 ) 
           {
               //until you here otherwise, number greater than 1 billion will be handled by program termination
               cout << endl << "File has number greater than 1 billion";
               cout << endl << "Too large number , terminating program!!! ";
               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 : ";
    
       while ( j < length )
       {
           output << sequence [ j ++ ] << " " ;
           if ( j % 7 == 0 )
               output << endl;
       }
       if ( isChainedSequence )
           output << endl << "Is a chained sequence " << endl;
    
       else
           output << endl << "is a not a chained sequence " << endl;;
    
       output << endl <<"****************************************************" << endl;
    
    }
    Regards

  2. #2
    Join Date
    Oct 2005
    Location
    Maady
    Posts
    1,819
    Look again in this function :

    Code:
    if ( temp != -1 )
           {
               //until you here otherwise, bad file format will be handled by program termination.
               cout << endl << "File has a sequence length greater than 10!!!";
               cout << endl << "Bad file format , terminating program!!! ";
               return 1;
           }
    return will make the programme exit before go to print() function .
    Programmer&Cracker CS
    MyBlog:Blog.Amahdy.com
    MyWebsite:www.Amahdy.com

Similar Threads

  1. Java Applet Compiler problem?
    By mdl in forum Java
    Replies: 3
    Last Post: 03-07-2005, 02:34 AM
  2. Vector and StreamTokenizer problem
    By dagma20 in forum Java
    Replies: 5
    Last Post: 02-23-2005, 12:13 AM
  3. Replies: 0
    Last Post: 02-11-2005, 03:49 PM
  4. Reliability Problem
    By elise in forum Java
    Replies: 0
    Last Post: 10-30-2002, 04:40 AM
  5. Replies: 0
    Last Post: 06-23-2000, 02:17 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links