0

My rand() gives out the same number (GPA) for every student.

srand(time(NULL));
    int gpa = 0 + (rand() % (10 - 0 + 1));

    for (int i = 0; i < number; i++) {
        cout << "Enter the student #" << i + 1 << "'s name: ";
        getline(cin, pStudents[i]); cout << endl;
    }
    for (int i = 0; i < number; i++) {
        cout << "Student " << pStudents[i] << " has GPA of: " << gpa << endl;
    }
rioV8
  • 24,506
  • 3
  • 32
  • 49
Tri_Hai05
  • 7
  • 2
  • 3
    Why not use what is available in ``? – PaulMcKenzie Mar 31 '22 at 14:01
  • 3
    Please descrive your expected behavior and actual behavior. Please post a [Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example) without any undeclared variables. – MikeCAT Mar 31 '22 at 14:01
  • 1
    What does "isn't really working" mean? What do you expect to happen? What's happening instead? One possibility: If you expect each student to have a different GPA, you'll need to call `rand()` more than once instead of precomputing a single value for `gpa` and assigning it to every single student. – Nathan Pierson Mar 31 '22 at 14:01
  • it is the rand() which gives out the same number of GPA for every student. – Tri_Hai05 Mar 31 '22 at 14:05
  • 2
    This code only computes `gpa` once. Sounds like perhaps you want to compute it for each iteration of your last `for` loop. – AndyG Mar 31 '22 at 14:05
  • Yes Andy. But how can i have a different random number GPA for every student . – Tri_Hai05 Mar 31 '22 at 14:09
  • 2
    Also, `0 + (rand() % (10 - 0 + 1));` can probably just be `rand() % 10 + 1` – 001 Mar 31 '22 at 14:10
  • Assigning students random grade point averages (GPA), (and one that goes from 0 to 10 rather than 0 to 4 at that), is not a great way to make an understandable example. It's kind of surreal conceptually. – Wyck Mar 31 '22 at 14:11
  • Call `srand` only once at the beginning of `main`. Read this: https://stackoverflow.com/questions/7343833/srand-why-call-it-only-once – Jabberwocky Mar 31 '22 at 14:12
  • `int gpa = 0 + (rand() % (10 - 0 + 1));` does not declare a symbolic expression, to be evaluated on each call to `gpa`. If you want something like that, you should use a function `int gpa() { return 0 + (rand() % (10 - 0 + 1)); }`. As is, your code calculates GPA once, and reuses the same value on every loop iteration. – JohnFilleau Mar 31 '22 at 14:14
  • hahaha thanks John. That is way more convenient. – Tri_Hai05 Mar 31 '22 at 14:14
  • @Tri_Hai05 The `rand()` function is poor. Use the updated C++ functions, as [shown here](http://coliru.stacked-crooked.com/a/e555508002948641) – PaulMcKenzie Mar 31 '22 at 14:31

1 Answers1

0

You only compute it once.

Here is how to fix it on your code:

srand(time(NULL));
for (int i = 0; i < number; i++) {
    cout << "Enter the student #" << i + 1 << "'s name: ";
    getline(cin, pStudents[i]); cout << endl;
}
for (int i = 0; i < number; i++) {
    int gpa = 0 + (rand() % (10 - 0 + 1));
    cout << "Student " << pStudents[i] << " has GPA of: " << gpa << endl;
}
Aethereal
  • 183
  • 1
  • 10