A small problem in the output

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Dec 2006
Posts: 1
Reputation: the_master is an unknown quantity at this point 
Solved Threads: 0
the_master the_master is offline Offline
Newbie Poster

A small problem in the output

 
0
  #1
Dec 16th, 2006
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 :
  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
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,273
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 378
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: A small problem in the output

 
0
  #2
Dec 16th, 2006
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.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,273
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 378
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: A small problem in the output

 
0
  #3
Dec 16th, 2006
Look here

  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. }
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 598
Reputation: SpS is on a distinguished road 
Solved Threads: 32
SpS's Avatar
SpS SpS is offline Offline
Posting Pro

Re: A small problem in the output

 
0
  #4
Dec 16th, 2006
  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.
  1. while( input )
  2. {
  3. ...
Last edited by SpS; Dec 16th, 2006 at 7:14 am.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC