Lost Password?

Go Back   CodeCall Programming Forum > Software Development > C and C++

C and C++ C and C++ forum for discussing all forms of C except for C#. These languages are powerful low level languages used for creating Operating Systems, Device Drivers, compilers and much more.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 12-13-2006, 09:04 AM
The_Master The_Master is offline
Newbie
 
Join Date: Dec 2006
Posts: 4
Rep Power: 0
The_Master is on a distinguished road
Default 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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #2 (permalink)  
Old 12-13-2006, 10:04 AM
Void's Avatar   
Void Void is offline
Programming Expert
 
Join Date: Jun 2006
Posts: 410
Rep Power: 10
Void is on a distinguished road
Default

Without looking to in depth into you program it looks like your program will try to read forever if no -1 is found. I suggest that you read all of the input into one string and then split the string by a -1. This would keep you from needing an ending -1.
__________________
Void
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 12-13-2006, 10:57 AM
The_Master The_Master is offline
Newbie
 
Join Date: Dec 2006
Posts: 4
Rep Power: 0
The_Master is on a distinguished road
Default

could you please write me the code because i'm very depressed and i dont know what to do
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 12-13-2006, 12:04 PM
WingedPanther's Avatar   
WingedPanther WingedPanther is offline
Super Mod
 
Join Date: Jul 2006
Age: 35
Posts: 1,751
Last Blog:
Game software (GURPS)
Rep Power: 24
WingedPanther is a name known to allWingedPanther is a name known to allWingedPanther is a name known to allWingedPanther is a name known to allWingedPanther is a name known to allWingedPanther is a name known to all
Default

It sounds like it's not picking up the EOF.
__________________
CodeCall Blog | CodeCall Wiki | Shareware | Linux Forum
Chat with other CodeCall members on IRC; connect to irc.codecall.net and join #codecall
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Peculiar UI Problem Needs Tackling adriyel C# Programming 2 04-06-2008 07:46 AM
i have a problem please help me!!!???? stack Java Help 8 09-22-2007 03:17 PM
[C] Comparison problem Alhazred C and C++ 1 08-29-2007 04:58 AM
Download Problem meimei3936 ionFiles 1 06-30-2007 04:00 PM
null exception problem connor7777 C# Programming 2 03-28-2007 11:37 AM


All times are GMT -5. The time now is 05:52 PM.

Contest Stats

John ........ 87.50000
dargueta ........ 75.00000
Xav ........ 50.00000
MeTh0Dz ........ 20.00000
gaylo565 ........ 18.00000
Johnnyboy ........ 3.00000

Contest Rules

Ads