944,134 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1588
  • C++ RSS
Dec 16th, 2006
0

A small problem in the output

Expand Post »
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 :
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <fstream>
  3. using namespace std;
  4. bool differsByOneDigit ( int , int );
  5. void outputResults ( ostream & , const int * , int , bool );
  6.  
  7. int main()
  8. {
  9.  
  10. ifstream input ( "c:\\data.txt" );
  11. ofstream output ( "c:\\out.txt" );
  12.  
  13. if ( input.fail() || output.fail() )
  14. {
  15. cout <<"input or output file did not open!!! " << endl;
  16. return 1;
  17. }
  18. int first , next ; //32bit int == 2147483647
  19. int sequence [ 10 ];//sequence read
  20. int seqLength = 0; //sequence length
  21. int j;
  22. int temp;
  23. bool match; //matching sequence
  24. int firstSeq;
  25.  
  26. if ( ! ( input >> temp ) )
  27. {
  28. //until you here otherwise, bad file format will be handled by program termination.
  29. cout << "File has a non-int within" << endl;
  30. cout << "Bad file format , terminating program!!! "<< endl ;
  31. return 1;
  32. }
  33. else if ( (temp > 1000000000) || (temp < -1 ) )
  34. {
  35. //until you here otherwise, out of range number will be handled by program termination
  36. cout << "File has out of range number "<< endl;
  37. cout << " TERMINATING program!!! "<< endl;
  38. return 1;
  39. }
  40. while ( ! input.eof() )
  41. {
  42. firstSeq = temp;
  43. //read next sequence
  44. while ( temp != -1 && seqLength < 10 && !input.eof() )
  45. {
  46. sequence [ seqLength ++ ] = temp;
  47. if ( ! ( input >> temp ) )
  48. {
  49. //until you here otherwise, bad file format will be handled by program termination.
  50. cout << "File has a non-int within"<< endl;
  51. cout << "Bad file format , terminating program!!! "<< endl ;
  52. return 1;
  53. }
  54. else if ( (temp > 1000000000) || (temp < -1 ) )
  55. {
  56. //until you here otherwise, out of range number will be handled by program termination
  57. cout << "File has out of range number "<< endl;
  58. cout << " TERMINATING program!!! " << endl;
  59. return 1;
  60. }
  61. }
  62. if ( temp != -1 )
  63. {
  64. //until you here otherwise, bad file format will be handled by program termination.
  65. cout << "File has a sequence length greater than 10!!!"<< endl;
  66. cout << "Bad file format , terminating program!!! "<< endl;
  67. return 1;
  68. }
  69. if ( seqLength <= 1 )
  70. {
  71.  
  72. cout << "Sequence of one or less found!!"<< endl ;
  73. cout << "Bad file format , terminating program!!! " << endl;
  74. return 0;
  75.  
  76. }
  77. sequence [ seqLength ] = -1;
  78. j = 0;
  79. first = sequence [ j ];
  80. next = sequence [ j + 1 ];
  81. j += 2;
  82. match = true;
  83. while ( match && next != -1)
  84. {
  85. match = differsByOneDigit( first , next );
  86. first = next;
  87. next = sequence [ j ];
  88. j++;
  89. }
  90. if ( match )
  91. match = differsByOneDigit ( first , firstSeq ); //check front to back
  92. outputResults ( output , sequence , seqLength , match );
  93. if ( ! ( input >> temp ) )
  94. {
  95. if ( !input.eof() )
  96. {
  97. //until you here otherwise, bad file format will be handled by program termination.
  98. cout << "File has a non-int within"<< endl ;
  99. cout << "Bad file format , terminating program!!! "<< endl;
  100. return 1;
  101. }
  102. }
  103. else if ( (temp > 1000000000) || (temp < -1 ) )
  104. {
  105. //until you here otherwise, out of range number will be handled by program termination
  106. cout << "File has out of range number "<< endl ;
  107. cout << "TERMINATING program!!! "<< endl;
  108. return 1;
  109. }
  110. seqLength = 0;
  111. }
  112. output.close();
  113. input.close();
  114. return 0;
  115. }
  116. bool differsByOneDigit ( int first , int next )
  117. {
  118. int differentDigits = 0;
  119. while ( first != 0 && next != 0 && differentDigits <= 1 )
  120. {
  121. if ( first % 10 != next % 10 ) //count different digits
  122. differentDigits ++;
  123. first /= 10;
  124. next /= 10;
  125. }
  126. if ( differentDigits > 1 || first || next )
  127. return false;
  128. else
  129. return true;
  130. }
  131. void outputResults ( ostream & output , const int * sequence , int length , bool isChainedSequence )
  132. {
  133. int j = 0;
  134. output << "The sequence : ";
  135. cout << "The sequence : ";
  136. while ( j < length && sequence [ j ] != -1 )
  137. {
  138. output << sequence [ j ] << " " ;
  139. cout << sequence [ j ++ ] << " " ;
  140. if ( j % 7 == 0 )
  141. {
  142. output << endl;
  143. cout << endl;
  144. }
  145. }
  146. if ( isChainedSequence )
  147. {
  148. output << " is chained. " << endl;
  149. cout << endl << "has been found....OPEN the output file to CHECK whether is it chained or not" << endl;
  150. }
  151. else
  152. {
  153. output << " is not chained. " << endl;
  154. cout << endl << "has been found....OPEN the output file to CHECK whether is it chained or not" << endl;
  155. }
  156. output << endl <<"****************************************************" << endl;
  157. cout << endl <<"****************************************************" << endl;
  158. }
REGARDS
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
the_master is offline Offline
1 posts
since Dec 2006
Dec 16th, 2006
0

Re: A small problem in the output

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.
Last edited by iamthwee; Dec 16th, 2006 at 6:45 am.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Dec 16th, 2006
0

Re: A small problem in the output

Look here

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7. int array[] = {121,122,121,122,123};
  8.  
  9. int size = 5;
  10.  
  11. int count = 0;
  12. int tmp, tmpTwo;
  13. for ( int i = 0; i < size; i++ )
  14. {
  15. tmp = array[i];
  16. tmpTwo = array[i+1];
  17. if (( tmp - tmpTwo == 1 )||( tmp - tmpTwo == -1 ))
  18. {
  19. count++; //increment the counter
  20. }
  21. }
  22.  
  23. if (count == size - 1 )
  24. {
  25. cout << "Your sequence is chained";
  26. }
  27. else
  28. cout << "Your sequence aint chained!";
  29.  
  30. cin.get();
  31. }
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Dec 16th, 2006
0

Re: A small problem in the output

C++ Syntax (Toggle Plain Text)
  1. while ( ! input.eof() )
  2. {
  3. ...
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.
C++ Syntax (Toggle Plain Text)
  1. while( input )
  2. {
  3. ...
Last edited by SpS; Dec 16th, 2006 at 7:14 am.
SpS
Reputation Points: 70
Solved Threads: 32
Posting Pro
SpS is offline Offline
598 posts
since Aug 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: How to start
Next Thread in C++ Forum Timeline: Sorting from file





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC