Strange output after reading from a file
<p>Using this code, the following execution yields strange results:</p>

<pre><code>C 100
R
W
</code></pre>

<p>The text file's first line defines the number of elements to read from it, and it contains a few values under 15, but every time I run this, the first value in my array is always printed out as 87 (the ASCII value for 'W'). If I change the 'W' functionality to 'X', then the first result in the array is 88.</p>

<pre><code>#include &lt;iostream&gt;
#include &lt;fstream&gt;

using namespace std; 

int arrayLength;

class ELEMENT
{
    public:
        int key;
};

class HEAP
{
    public:
        int capacity;
        int size;
        ELEMENT H [];
};

HEAP initialize(int n)
{
    HEAP h;
    h.capacity = n;
    h.size = 0;
    return h;
}

void buildHeap(HEAP &amp;h, ELEMENT *a)
{
    h.size = arrayLength;
    for (int i = 1; i &lt;= arrayLength; i++)
    {
        h.H[i] = a[i];
    }

    for (int i = h.size/2; i &gt;= 1; i--)
    {
        // HEAPIFY HERE
    }
}

void printHeap(HEAP &amp;h)
{
    cout &lt;&lt; "Capacity:\t" &lt;&lt; h.capacity &lt;&lt; endl;
    cout &lt;&lt; "Size:\t\t" &lt;&lt; h.size &lt;&lt; endl;
    cout &lt;&lt; "|";
    for (int i = 1; i &lt;= h.size; i++)
    {
        cout &lt;&lt; " ";
        cout &lt;&lt; h.H[i].key &lt;&lt; " |";
    }
    cout &lt;&lt; endl;
}

int main()
{
    char c;
    int val;
    HEAP h;

    while (c != 'S')
    {
        cin &gt;&gt; c;
        switch (c)
        {
            case 'S':
                break;
            case 'C':
                cin &gt;&gt; val;
                h = initialize(val);
                break;
            case 'W':
                printHeap(h);
                break;
            case 'R':
                {
                    ifstream infile;
                    infile.open("HEAPinput.txt");
                    infile &gt;&gt; arrayLength;
                    ELEMENT* a = new ELEMENT[arrayLength];
                    for (int i = 1; i &lt;= arrayLength; i++)
                        infile &gt;&gt; a[i].key;
                    infile.close();
                    buildHeap(h, a);
                }
                break;
        }
    }

    return 0;
}
</code></pre>

<p>It is being compiled using g++ on a Unix server.</p>

<p>EDIT:
To clarify:
With a text file with the following contents (space = new line):</p>

<pre><code>12 9 10 11 12 8 7 6 5 4 3 2 1
</code></pre>

<p>The output is:</p>

<pre><code>Capacity: 100

Size: 12

| 87 | 10 | 11 | 12 | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 |
</code></pre>

<p>So it's working except for that first element. </p>
