Reading Lines of Text From File C++ and Last Line Prints Twice
#i
"while" processing last line of input twice
Posted 08 October 2005 - 01:52 AM
When I use this lawmaking in the input file:
infile.txt 2 1 3 2 4 3 i six 8 v 7 iii nine 2 4 2 1 5 2 3
I get this output:
outfile.txt 1 i iii two 4 2 1 6 eight five 7 3 ix 2 4 3 1 5 2 3 four i five 2 3
Here is the relevant code:
class Magic_Square { private: int Size; // Number of rows and columns in the Magic Square array int MagSq[five][5]; // Magic Square matrix int ColSums[5]; // Assortment of column sums int RowSums[5]; // Array of rowsums int Diag1; // Sum of major diagonal int Diag2; // Sum of small-scale diagonal bool Sum_Criteria; // Was the sum exam met? bool Sequence_Criteria; // Was the sequence test met? void Calc_RowSums(); // Calculates the row sums void Calc_ColSums(); // Calculates the column sums void Calc_Diags(); // Calculates the two diagonal sums public: Magic_Square(); // Constructor to initialize ColSums, RowSums, Diag1, and Diag2 // to zeros void Input_Values(); // Inputs the matrix values void Output_Values(); // Displays the matrix values void Seq_Test(); // Determines if the sequence test was met void Sum_Test(); // Determines if the sum exam was met void Evaluate(); // Determines and displays whether a magic foursquare or non and if not, // which criteria failes. }; ifstream infile; ofstream outfile; int main() { int x, lines = 0; infile.open("infile.txt"); outfile.open("outfile.txt"); Magic_Square square; while(!infile.eof()) { lines += 1; outfile << lines << endl; square.Input_Values(); square.Output_Values(); } outfile.close(); infile.shut(); render 0; } Magic_Square::Magic_Square() { int temp; Size = 0; // Set ColSums and RowSums to 0 for(temp = 0; temp <= 4; temp++) { ColSums[temp] = 0; RowSums[temp] = 0; } // Set Diag1 and Diag2 to 0 Diag1 = 0; Diag2 = 0; } void Magic_Square::Input_Values() { int col, row; infile >> Size; for(col = 0; col < Size; col++) for(row = 0; row <= Size - 1; row++) { infile >> MagSq[col][row]; } } void Magic_Square::Output_Values() { int col, row; for(col = 0; col < Size; col++) { for(row = 0; row <= Size - one; row++) { outfile << MagSq[col][row] << " "; } outfile << "\north"; } outfile << "\n\n"; }
What I'm wondering is... why is this final line from the input file processing twice?
I've tried many things for the while loop in the main function, including while(infile);
while(infile.peek() != EOF);
while(!infile.eof());
etc.... merely zippo seems to work
Many cheers to the person who solves the riddle, as this is my first post in this forum
-Josh
#2
Re: "while" processing final line of input twice
Posted 08 October 2005 - 01:55 PM
well, i found a piece of work-around. Unfortunately, i dont know WHY what I had the first fourth dimension doesn't work, and why this does. Perchance somebody can enlighten me.
while(!infile.eof()) { square.Input_Values(); lines += ane; if(!infile.eof()) { outfile << lines << endl; square.Output_Values(); } }
I didn't even get any replies? blargh
#iii
Re: "while" processing last line of input twice
Posted 08 October 2005 - 02:31 PM
At a guess you have a new line at the terminate of the input file, which takes the line count to 4 lines.
Plus you practise non zero the values in the Magic Square, instead you zero the column and row sums which don't appear to be in use withal, betwixt outputs so when the 4th go happens it still contains the values from the previous run.
Just a thought without really trying your code in a compiler.
Cheers
Amos
#4
Re: "while" processing terminal line of input twice
Posted 04 October 2006 - 05:03 PM
Arnie,
I am interested in seeing the entire programme. if you wouldn't mind emailng it to me at [electronic mail protected]
IceCap
#5
Re: "while" processing last line of input twice
Posted 04 October 2006 - 06:34 PM
Instead of reading until end of file with while(!infile.eof() prefer while successfully reading input from a file and know that the state of the stream -- indicating successful input or a failure -- is available from the input operation with while ( infile >> i )
This postal service has been edited past Xing: 04 October 2006 - 06:35 PM
Source: https://www.dreamincode.net/forums/topic/12625-while-processing-last-line-of-input-twice/
0 Response to "Reading Lines of Text From File C++ and Last Line Prints Twice"
Post a Comment