Count array instances in struct
<p>I have a struct datatype. It contains a data member that is also a struct.</p>

<pre><code>struct List{
    private:
        struct Line listLine[MAX_LINES];
    public:
        void set(int no);
        void display() const;
};
</code></pre>

<p><code>listLine</code> is of Line type, defined as:</p>

<pre><code>struct Line {
    private:
        int num;
        char itemString[MAX_CHARS + 1];
    public:
        bool set(int n, const char* str);
        void display() const;
};
</code></pre>

<p>main.cpp</p>

<pre><code>int main() {

    int no;
    List list;

    cout &lt;&lt; "List Processor\n==============" &lt;&lt; endl;
    cout &lt;&lt; "Enter number of items : ";
    cin &gt;&gt; no;

    list.set(no);
    list.display();
}
</code></pre>

<p>line::display</p>

<pre><code>void Line::display() const {

    cout &lt;&lt; this-&gt;num &lt;&lt; ' ' &lt;&lt; this-&gt;itemString &lt;&lt; endl;

}
</code></pre>

<p>list.display (calls line.display, and line.display prints one instance of a line, if I call it 3 times, it will output 3 different types of data, each corresponding to an index of <code>listLine</code>)</p>

<pre><code>void List::display() const {

    for (int i = 0; i &lt; 10; i++) {
        this-&gt;listLine[i].display();
    }

}
</code></pre>

<p>Let's say that I populate <code>listLine</code> as so:  </p>

<pre><code>listLine[0].num = 1
listLine[0].itemString = ABC
listLine[1].num = 2
listLine[1].itemString = ZXC
listLine[2].num = 3
listLine[2].itemString = QWE
</code></pre>

<p>I know the above isn't proper C++ syntax, I'm just trying to illustrate the concept, show what data is inside <code>listLine</code>.</p>

<p>So far I have been able to print the contents of listLine as so:</p>

<pre><code>for (int i = 0; i &lt; 10; i++) {
    this-&gt;listLine[i].display();
}
</code></pre>

<p>Output of above</p>

<pre><code>// reading values in
List Processor
==============
Enter number of items : 3
Enter line number : 1
Enter line string : ABC
Accepted
Enter line number : 2
Enter line string : ZXC
Accepted
Enter line number : 3
Enter line string : QWE
Accepted

// printing those values with the loop i posted
1 ABC
2 ZXC
3 QWE
1 
134514883 
-1217566436 Jm·Z·Èí¬¿-
134515757 
134514257 
134515633 0
-1218833660 ôZ·F·
</code></pre>

<p>Notice how the only valid data is <code>1 ABC</code>, <code>2 ZXC</code>, and <code>3 QWE</code> and after that it just prints gibberish?</p>

<p>I want to display only the valid data, but the display() function of List takes no paramaters, so how am I supposed to know which data in <code>listLine</code> is valid?</p>

<p>I only want to print the valid lines, in the above example, that is three, with the other 7 lines being nonsense.</p>
