I'm trying to write a function that reads several numbers from a file (it interprets them as powers of X and coefficients and stores them in an array that represents a polynomial.)
Anyways, I want it to skip over lines that start with a '/' character, to provide a mechanism of including comments in the file. Here's the code:
Oh, and a quick note.
POWER and COEFFICIENT are enum as 0 and 1, respectively. MAX_SIZE is #define as 32767 (fairly arbitrary value.)
This works perfectly for files with only one comment line, for example, this file:
The output for the above file is this:
However, if a file includes multiple comments in it, like this one:
My output should look exactly the same, but instead I get this:
It doesn't print out that it has ignored the second comment line... it doesn't even attempt to read the rest of the file, instead it just skips straight to the end of the file?
I've tried placing the second comment in multiple places in the file, and no matter where I put it, it always skips straight to the EOF when it reaches it, without even printing that it is ignoring a comment line.
Any suggestions on why it's doing this or how I can fix it to behave the way I want it to?
Anyways, I want it to skip over lines that start with a '/' character, to provide a mechanism of including comments in the file. Here's the code:
Oh, and a quick note.
POWER and COEFFICIENT are enum as 0 and 1, respectively. MAX_SIZE is #define as 32767 (fairly arbitrary value.)
Code:
// Read the file.
int reading = POWER;
int Xpower = 0;
long double Xcoefficients[MAX_SIZE] = { 0.0 };
long double diffTerm = 0.0;
long double mostNegDiffTerm = 0.0, mostPosDiffTerm = 0.0;
for (int i = 0; i < (MAX_SIZE*2); i++)
{
#ifdef NDEBUG
[b]cout << "Running..." << endl;[/b]
#endif
[b]if (file.peek() == '/')[/b]
{
#ifdef NDEBUG
[b]cout << "Ignoring..." << endl;[/b]
#endif
[b]file.ignore(MAX_SIZE, '\n');
file.clear(); // I've tried both with and without this line - behavior does not change.[/b]
}
switch (reading)
{
case COEFFICIENT:
file >> Xcoefficients[Xpower];
diffTerm = Xcoefficients[Xpower]*Xpower;
mostNegDiffTerm = (diffTerm<mostNegDiffTerm)?diffTerm:mostNegDiffTerm;
mostPosDiffTerm = (diffTerm>mostPosDiffTerm)?diffTerm:mostPosDiffTerm;
reading = POWER;
break;
case POWER: default:
file >> Xpower;
polyDegree = (Xpower>polyDegree)?Xpower:polyDegree;
reading = COEFFICIENT;
break;
}
[b]if (file.peek() == EOF)[/b]
{
#ifdef NDEBUG
[b]cout << "Reached EOF" << endl;[/b]
#endif
break;
}
}
This works perfectly for files with only one comment line, for example, this file:
Code:
// polynomial f(x) = 2.5x^3 + 3x^2 - 2.13x + 1
1 -2.13
0 1
2 3.0
3 2.5
The output for the above file is this:
Code:
Running...
Ignoring...
Running...
Running...
Running...
Running...
Running...
Running...
Running...
Running...
Reached EOF
However, if a file includes multiple comments in it, like this one:
Code:
// polynomial f(x) = 2.5x^3 + 3x^2 - 2.13x + 1
1 -2.13
0 1
//4 26
2 3.0
3 2.5
My output should look exactly the same, but instead I get this:
Code:
Running...
Ignoring...
Running...
Running...
Running...
Running...
Reached EOF
It doesn't print out that it has ignored the second comment line... it doesn't even attempt to read the rest of the file, instead it just skips straight to the end of the file?
I've tried placing the second comment in multiple places in the file, and no matter where I put it, it always skips straight to the EOF when it reaches it, without even printing that it is ignoring a comment line.
Any suggestions on why it's doing this or how I can fix it to behave the way I want it to?