I am self-learning C++ from a text book and I have a problem to solve, outlined below.
I have the following class structure:
#include <iostream>
#include <cstdio>
#include <ctime>
using namespace std;
class classroom{
char name[25];
int student_id;
float grades[10];
float average;
int num_tests;
float letter_grade;
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();
};
And I have the following Constructor for the above class:
classroom::classroom(){
int i;
srand((unsigned)time(0));
int random_integer=0;
random_integer = (rand()%5) + (rand()%5);
num_tests=0;
average=0.0;
for(i=0;i<10;i++){
grades[i]=0.0;
}
for(i=0;i<27;i++){
name[i]='-';
}
cout<<"\n*****************Finished*****************";
}
There will be 3 students of this class structure declared in the main:
int main()
{
classroom students[3];
//and so...
}
I need to generate a unique student ID for each student in the constructor within a range of values, say, 0 to 10.
I have copied the following code snippet into the constructor. It generates my random number for me within the desired range:
srand((unsigned)time(0));
int random_integer=0;
random_integer = (rand()%5) + (rand()%5);
The problem is that I need to get rid of any duplicates within the range of random numbers that are generated.
Why does the number need to be random? Can’t you just use a static int that gets incremented every time you need to generate a new student number?