Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It looks like you want something similar to this:</p> <pre><code>#include &lt;iostream&gt; using namespace std; int main() { int standard, count1, count2, count3, count4, count5; // Initialize your int's standard = count1 = count2 = count3 = count4 = count5 = 0; do { cout&lt;&lt; "Enter value: "; cin &gt;&gt; standard; // Switch through your cases switch(standard) { case 1: count1++; break; case 2: count2++; break; case 3: count3++; break; case 4: count4++; break; case 5: count5++; break; } } while(standard != 0); // Do this type of loop for all your count vars for(int i=0;i&lt;count1;++i) cout&lt;&lt; "1"; for(int i=0;i&lt;count2;++i) cout&lt;&lt; "2"; // And so on... return 0; } </code></pre> <p>For the for loops, note that you are going from 0 to <code>&lt;</code> the total number. If you went to <code>&lt;=</code> you would go passed that number. The range <code>[0,4]</code> = 5 (or in our case <code>[0,5)</code> for the set of integers) and so does <code>[1,5]</code> so remember to mind your offsets. Good luck!</p> <p><strong>EDIT:</strong> Also recognize that the position of the <code>switch</code> statement has been moved. By placing it in the loop, you actually count every time it runs through the loop. For instance, the loop "blocks" (in other words, it waits for input) at your <code>cin</code> line. When it receives an input (though you have no error-checking, so a non-int value may cause a segfault), it will then check it against the switch statement and increment accordingly.</p> <p>By placing the <code>switch</code> statement outside the loop (as you initially had it), please realize that nothing happens. You have no <code>default</code> condition in your <code>switch</code> statement, nor a case for handling <code>0</code>. If you check your <code>switch</code> after the loop, <code>standard == 0</code> since that is how you exited the loop in the first place (therefore, standard's last value will be retained).</p> <p>Furthermore, revisit the for loop syntax. This can be done in several ways, there are many good articles here on SO which can help you further by using the search function, but it basically works like this:</p> <pre><code>for(STARTING_POINT;SOME_CONDITION;SOME_CHANGE); </code></pre> <p>STARTING_POINT = The value where you should start your loop SOME_CONDITION = When this condition is true, then the loop will terminate SOME_CHANGE = how to change your variable (i.e. from starting point) until it reaches the condition</p> <p>So a loop which looks like this:</p> <pre><code>for(int i=1;i&lt;=5;++i){ ... } </code></pre> <p>means to initialize <code>int i</code> to value <code>1</code> Until <code>i &lt;= 5</code> run through your loop Then, on every iteration, increment <code>i</code> by one (which is <code>++i</code> or <code>i++</code>)</p> <p>Finally, as you can see, I added an initialization line to my int's. What this does is it gives your variables some starting value rather than garbage value.</p> <p>I hope this helps</p> <p>Regards,<br /> Dennis M.</p>
    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. This table or related slice is empty.
    1. 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