#include <iostream> #include <string> #include <fstream> #include <random> #incl
ID: 3721786 • Letter: #
Question
#include <iostream>
#include <string>
#include <fstream>
#include <random>
#include <ctime>
#include <stdlib.h>
using namespace std;
const int MAX_STUDENTS = 30;
const int MAX_ASSIGNMENTS = 10;
void startUp(int assignments[MAX_STUDENTS][MAX_ASSIGNMENTS], string studentNames[MAX_STUDENTS], int &numStudents, int &numAssignments) {
//this loads from file
ifstream in("students.txt");
if (in.is_open()) {
in >> numStudents;
in >> numAssignments;
for (int i = 0; i < numStudents; ++i) {
in >> studentNames[i];
}
for (int i = 0; i < numStudents; ++i) {
for (int j = 0; j < numAssignments; ++j) {
in >> assignments[i][j];
}
}
in.close();
}
}
void displayGradebook(int assignments[MAX_STUDENTS][MAX_ASSIGNMENTS], string studentNames[MAX_STUDENTS], int numStudents, int numAssignments) {
// prints the gradebook to the screen.
for (int i = 0; i < numStudents; i++) {
cout << studentNames[i] << " ";
if (studentNames[i].size() < 8) {
cout << " ";
}
for (int j = 0; j < numAssignments; j++) {
cout << assignments[i][j] << " ";
}
cout << endl;
}
}
void diplayMenu() {
cout << "Do you want to add a (s)tudent, add an (a)ssignment, (c)urve a grade, (q)uit, or choose a (r)andom student? ";
}
void addStudent(int assignments[MAX_STUDENTS][MAX_ASSIGNMENTS], string studentNames[MAX_STUDENTS], int &numStudents, int numAssignments) {
// adds student to the grade book
cout << "Please enter the new student's name: ";
cin >> studentNames[numStudents];
// asks for the grade for the new student
for (int i = 0; i < numAssignments; i++) {
cout << "Please enter " << studentNames[numStudents] << "'s grade for assignment " << i + 1 << ": ";
cin >> assignments[numStudents][i];
}
numStudents++;
}
void addAssignment(int assignments[MAX_STUDENTS][MAX_ASSIGNMENTS], string studentNames[MAX_STUDENTS], int numStudents, int &numAssignments) {
// allows you to add assignment for students
for (int i = 0; i < numStudents; i++) {
cout << "Please enter " << studentNames[i] << "'s grade for the new assignemnt: ";
cin >> assignments[i][numAssignments];
}
numAssignments++;
}
void curveGrade(int assignments[MAX_STUDENTS][MAX_ASSIGNMENTS], int numStudents, int numAssignments) {
int assignmentToCurve = -1;
cout << "Which Assignment do you wish to curve (1-" << numAssignments << "): ";
cin >> assignmentToCurve;
assignmentToCurve--;
int sum = 0;
for (int i = 0; i < numStudents; i++) {
sum += assignments[i][assignmentToCurve];
}
float average = (float)sum / numStudents;
if (average < 70) {
for (int i = 0; i < numStudents; i++) {
assignments[i][assignmentToCurve] += (70 - average);
}
cout << "curve of " << (int)(70 - average) << " applied!" << endl;
}
else {
cout << "No curve needed!" << endl;
}
}
void shutDown(int assignments[MAX_STUDENTS][MAX_ASSIGNMENTS], string studentNames[MAX_STUDENTS], int numStudents, int numAssignments) {
// this opens the last file that was created if the user chooses y
ofstream out("students.txt");
out << numStudents << endl;
out << numAssignments << endl;
for (int i = 0; i < numStudents; ++i) {
out << studentNames[i] << endl;
}
for (int i = 0; i < numStudents; ++i) {
for (int j = 0; j < numAssignments; ++j) {
out << assignments[i][j] << " ";
}
out << endl;
}
out << endl;
out.close();
}
// chooses a random student from the gradebook
void randomStudent(int numStudents, string studentNames[MAX_STUDENTS])
{
std::random_device rdev;
default_random_engine randomStudent(rdev());
uniform_int_distribution<int> studentNames(1, numStudents);
cout << "You randomly selected: " << studentNames(randomStudent) << endl;
}
int main()
{
int numAssignments = 0;
int numStudents;
int assignments[MAX_STUDENTS][MAX_ASSIGNMENTS];
string studentNames[MAX_STUDENTS];
char choice;
cout << "Do you want to load from file(y or n)? ";
cin >> choice;
if (choice == 'y') {
startUp(assignments, studentNames, numStudents, numAssignments);
}
else {
cout << "Please enter the number of students currently enrolled (max " << MAX_STUDENTS << "): ";
cin >> numStudents;
if (numStudents > 30) {
numStudents = 30;
cout << "The number of students has been reduced to the max " << MAX_STUDENTS << "." << endl;
}
for (int i = 0; i < numStudents; i++) {
cout << "Enter student " << i + 1 << "'s name: ";
cin >> studentNames[i];
}
}
char command = ' ';
while (command != 'q') {
system("CLS");
displayGradebook(assignments, studentNames, numStudents, numAssignments);
diplayMenu();
cin >> command;
system("CLS");
switch (command) {
case 's':
addStudent(assignments, studentNames, numStudents, numAssignments);
break;
case 'a':
addAssignment(assignments, studentNames, numStudents, numAssignments);
break;
case 'c':
curveGrade(assignments, numStudents, numAssignments);
break;
case 'r':
// this will allow the user to randomly choose a student.
system("CLS");
randomStudent(numStudents, studentNames);
break;
case 'q':
break;
default:
cout << command << " is an invalid command!" << endl;
break;
}
system("PAUSE");
}
shutDown(assignments, studentNames, numStudents, numAssignments);
}
Explanation / Answer
In Above program it is not generating random name Because there is some declaration issue Which i have mentioned below:
In function 'void randomStudent(int,std::string*)';
Declaration of 'std::uniform_int_distribution studentNames' shadows a parameter
uniform_int_distribution<int> studentNames(1,numStudents);
In function 'int main()';
Ignoring return value of 'int system(const char*)',declaration with attribute warn_unused_result.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.