我的 rand() 并没有真正在 C++ 中工作,特别是在 Visual Studio 2019 年。我确实有“包括 <time.h> 和 <stdlib.h>
My rand() isn't really working in C++ specifically in Visual Studio 2019. I do have had "include <time.h> and <stdlib.h>
我的 rand() 为每个学生给出相同的数字 (GPA)。
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;
}
您只需计算一次。
以下是如何在您的代码中修复它:
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;
}
我的 rand() 为每个学生给出相同的数字 (GPA)。
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;
}
您只需计算一次。
以下是如何在您的代码中修复它:
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;
}