Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <pre><code>if (i == poly.end()) { // if we reached the last term </code></pre> <p>This comment shows your error. For any given collection of items, <code>items.end()</code> returns the entry after the last item.</p> <p>For instance, say I have a 5-item std::vector:</p> <pre><code>[0] [1] [2] [3] [4] </code></pre> <p>Then begin() points to:</p> <pre><code>[0] [1] [2] [3] [4] /\ </code></pre> <p>And end() points to:</p> <pre><code>[0] [1] [2] [3] [4] [] /\ </code></pre> <p>Your for loop, it looks like:</p> <pre><code>for (i=poly.begin() ; i != poly.end(); i++ ) </code></pre> <p>Note that comparing <code>i</code> to <code>poly.end()</code> happens before iter is used. As soon as <code>i == poly.end()</code>, you're done. </p> <p>Your code inside of <code>if (i == poly.end()) {</code> will never be executed because this can never be true.</p> <p>You can test for the end using the following:</p> <pre><code>// get access to the advance function #include &lt;iterator&gt; .... std::list&lt;term&gt;::iterator endCheckIter = i; std::advance(endCheckIter, 1); if (endCheckIter == poly.end()) { ... } </code></pre> <p>But a simpler way might be:</p> <pre><code>std::list&lt;term&gt;::iterator endCheckIter = i; ++endCheckIter; if (endCheckIter == poly.end()) { ... } </code></pre> <p>Edit: I'm not sure why you're getting garbage. Add in your missing braces and handle the non-end case, and everything works here:</p> <pre><code>void print(char var) { list&lt;term&gt;::iterator endCheckIter; for (i=poly.begin() ; i != poly.end(); i++ ) { // &lt;- MISSING BRACE //going through the entire list to retrieve the terms and print them endCheckIter = i; ++endCheckIter; cout &lt;&lt; i-&gt;coef &lt;&lt; var &lt;&lt; "^" &lt;&lt; i-&gt;pow; // &lt;- MISSING OUTPUT if (endCheckIter != poly.end()) { if (i-&gt;coef &gt; 0) cout &lt;&lt; " + "; else cout &lt;&lt; " - "; } } // &lt;- MISSING BRACE } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload