Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>One way to do this is to use what is called a <strong>static member variable</strong>. Usually, each object gets <em>its own</em> copy of the member variables. In your code, each student will have his own copy of <code>name[25]</code>, <code>student_id</code>, etc. But what you want, is something that will be shared between each instance of the class (each of the elements in <code>classroom students[3]</code> are instances of the <code>classroom</code> class.)</p> <p>Here are some notes on <a href="http://www.learncpp.com/cpp-tutorial/811-static-member-variables/" rel="nofollow">static member variables</a>. Note that they present a solution to a very similar problem -- they assign unique IDs to each class instance. The only difference between what they present here and what you ask for is that you ask for it to be randomized, instead of consecutive.</p> <p>So instead of a static member variable keeping track of one number (the last one assigned), you will want your static member variable to keep track of <em>all of the previously assigned ids</em>. A std.vector or simple int array should work.</p> <hr> <pre><code>#include &lt;iostream&gt; #include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; #include &lt;time.h&gt; #include &lt;vector&gt; using namespace std; class classroom{ char name[25]; int student_id; float grades[10]; float average; int num_tests; float letter_grade; static vector&lt;int&gt; used; public: void enter_name_id(void); void enter_grade(void); void average_grades(void); void letter_grades(void); void output_name_id_grade(void); classroom(); }; vector&lt;int&gt; classroom::used = vector&lt;int&gt;(); classroom::classroom(){ int i; int random_integer=0; bool rand_ok; do { // Generate Random Integer random_integer = (rand()%5) + (rand()%5); //cout &lt;&lt; "Generated: " &lt;&lt; random_integer &lt;&lt; endl; rand_ok = true; for(i=0; i&lt;used.size(); i++) { if(used[i] == random_integer){ rand_ok = false; break; } } } while (rand_ok == false); // If we get here, random_integer is not in the used vector // therefore accept and store as student_id student_id = random_integer; // ... and update used vector used.push_back(student_id); num_tests=0; average=0.0; for(i=0;i&lt;10;i++){ grades[i]=0.0; } for(i=0;i&lt;27;i++){ name[i]='-'; } cout&lt;&lt;"*****************Finished*****************\n"; } int main() { // You should only seed the RNG once srand((unsigned)time(0)); classroom students[3]; } </code></pre> <p>The above code is one way you might do it, by storing the used student ids in a vector, and then every time you create a new student, ensure the randomly generated id doesn't match an already assigned ID.</p> <p>One other note, you should only seed the random number generator once (especially if your seed is the time). Since you were seeding it 3 times in such a short time, the random numbers being produced were the same.</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.
    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