Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 12-16-2006, 01:54 AM   #1 (permalink)
the_master
Recruit
 
Join Date: Dec 2006
Posts: 1
the_master is on a distinguished road
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. 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 :

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 << "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
the_master is offline   Reply With Quote
Old 12-17-2006, 09:09 AM   #2 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,545
Valmont is on a distinguished road
The first question that pops my mind is why do you need to enter "-1" whilst the "newline" already implicates a newline.

The second question is: how many sequences is the user allowed to enter in a single run? Twice? Or thrice? Shall we use "-1" to signal the end of all sequences?
So many choices...

But one thing is sure: the whole program should be revised.
__________________
Valmont is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
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
Small problem Crono254 PHP 1 03-13-2006 08:01 PM
NEED HELP on card assignment marina Standard C, C++ 9 05-03-2005 07:42 AM
Output problem with file manipulation (newbie) crazyant Java 4 03-11-2005 01:03 PM
looping problem maria_arif MS Technologies ( ASP, VB, C#, .NET ) 1 11-29-2004 08:07 AM
This is a windows/C problem UnderWing Standard C, C++ 6 03-28-2003 06:17 AM


All times are GMT -8. The time now is 05:22 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting