Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I see no code where you initialise <code>a</code> to 1 so it may be it has some arbitrary value. If that arbitrary value is greater than <code>num</code>, the outer loop will never start.</p> <p>For what it's worth, I would be using <code>for</code> loops in this case since you know in advance what the limits are, something like the following pseudo-code:</p> <pre><code># Top line for i = 1 to num (inclusive): output "*" output newline # Middle lines for i = 2 to num-1: output "*" # Left char for j = 2 to num-1: # Middle chars output " " output "*" and newline # Right char # Bottom line for i = 1 to num (inclusive): output "*" output newline </code></pre> <p>Then you don't have to worry about condition checking within the loop body.</p> <p>A good rule of thumb is to use <code>for</code> for a known-before-the-start count of iterations, <code>while</code> for a loop where you don't know in advance how often you'll iterate.</p> <p>Another likely problem is your condition:</p> <pre><code>while ( num &gt; 1 || num &lt; 20 ) </code></pre> <p>Regardless of the value of <code>num</code>, that is always true, since you're using logical-or <code>||</code>. Think of the possibilities:</p> <pre><code>num &lt;= 1 : false or true -&gt; true num == 2..19 : true or true -&gt; true num &gt;= 20 : true or false -&gt; true </code></pre> <p>If you want to continue looping while you have a value <em>outside</em> of the range 1..20, you should use:</p> <pre><code>while ( num &lt; 1 || num &gt; 20 ) </code></pre> <p>and you then end up with the following:</p> <pre><code>num &lt;  1     : true or false -&gt; true num == 1..20 : false or false -&gt; false num &gt;  20    : false or true -&gt; true </code></pre> <hr> <p>There are quite a few other potential problems with your code, to wit:</p> <ul> <li>You appear to have the outer loop in there twice.</li> <li>You don't appear to define <code>b</code> or <code>num</code>.</li> <li>You don't appear to set <code>num</code> before the outer loop (which checks it).</li> <li>I suspect you meant to close the <code>while ( num &gt; 1 || num &lt; 20 )</code> loop immediately after the <code>cin.ignore()</code> call since it's meant to keep going until you get a value from 1 to 20 and <em>then</em> draw the square. As it stands, a square will be drawn even if you enter 99.</li> </ul>
 

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