Can someone please help me finish writing this C++ code? I have to make an autom
ID: 3904306 • Letter: C
Question
Can someone please help me finish writing this C++ code? I have to make an automated gradebook. Here are the instructions and the code I have so far.
I need it took like it does in the console above. Here is my code so far:
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <math.h>
#include <string>
using namespace std;
// constants
const int SCORES_MAX = 5;
const int STUDENTS_MAX = 6;
// menu definition
enum MENU { EXIT = 0, ENTER_STUDENTS = 1, UPLOAD_GRADES = 2, PRINT_GRADES = 3 };
// letter grade factor of 10's
enum LETTER_GRADE { F=0, D=6, C=7, B=8, A=9 };
// Grade structure definition
struct Grade {
int score;
LETTER_GRADE letter;
};
// Student structure definition
struct Student {
string stuName;
Grade grades[SCORES_MAX];
};
// function declarations
int getStudents(struct Student students[]);
int getScores(struct Student students[], int count);
LETTER_GRADE getLetter(float score);
int main() {
/* initialize random generator
with current time as seed */
srand(time(NULL));
int numStudents = 0;
int scoresLoaded = 0;
struct Student gradebook[STUDENTS_MAX];
MENU choice;
do {
cout << "Auto Gradebook:" << endl
<< " (1) Enter students" << endl
<< " (2) Upload grades" << endl
<< " (3) Print grades" << endl
<< " (0) Exit" << endl
<< "> ";
int ask; // temp variable to prompt user
cin >> ask; // for a numeric menu choice
choice = MENU(ask); // convert numeric choice to MENU type
switch (choice) {
case ENTER_STUDENTS:
cin.ignore(); // remove any end-line character on the input stream
// left behind by "cin" if you use "getline()" in
// "getStudents()"
numStudents = getStudents(gradebook);
break;
case UPLOAD_GRADES:
if (numStudents == 0) {
cout << "Enter students first!";
}
else {
scoresLoaded = getScores(gradebook, numStudents);
cout << scoresLoaded << " scores uploaded.";
}
break;
case PRINT_GRADES:
if (numStudents == 0)
cout << "Enter students first!";
else
printGrades(gradebook, numStudents);
break;
case EXIT:
break;
default:
cout << "Invalid choice!";
}
cout << endl << endl;
}
while (choice != EXIT);
return 0;
} // ends main
/**
* Prompt the user to enter student names into the gradebook
* @param students - structure containing student name array
* @return int - new student amount
*/
int getStudents(struct Student students[]) {
int stuCount = 0;
string nameIn = "";
cout << "Enter grades for each student number (X to quit) ";
bool input = false;
while (input == false) {
cout << " Student name (" << stuCount << "):";
getline(cin >>ws, nameIn); // inputting names with whitespace
stuCount++;
if ((nameIn == "X") || (nameIn == "x") || (stuCount == STUDENTS_MAX))
input = true;
else
students[stuCount - 1].stuName = nameIn;
}
return stuCount;
}
/**
* Generate random test scores for all the students
* entered into the gradebook
* @param students - gradebook
* @param count - number of students in the gradebook
* @return int - the number of scores generated
*/
int getScores(struct Student students[], int count) {
int scores = 0;
for (int stu = 0; stu < count; stu++) {
for (int grade = 0; grade < SCORES_MAX; grade++) {
int tens = rand() % 6; // 0..5
int % 10; // 0..9
int score = (tens + 4) * 10 + ones; // 40..99
students[stu].grades[grade].score = score;
students[stu].grades[grade].letter = getLetter(score);
scores++;
}
}
return scores;
} // end getScores
/**
* Convert a numeric score to a letter grade.
* @param score - the exam score
* @return LETTER_GRADE - the appropriate letter grade
*/
LETTER_GRADE getLetter(float score) {
LETTER_GRADE grade = F; // an F for anything below 60
/* see if the rounded score is at least an integer 59
ceiling rounds and truncate to an int */
if (ceil(score) >= 59) {
/* Round the score and add 1 as the random
functions values were 0..desired range-1
then see how many 10's would go into the score */
int nScore = (ceil(score) + 1) / 10; // integer division by 10's
/* cast the tens digit of the score
to an enum based on ordinal value */
grade = LETTER_GRADE(nScore);
}
return grade;
}
/**
* Print the letter grade for each student
* @param LETTER_GRADE - score
* @return char - Correct letter grade for student
*/
char printLetter(float score) {
score = getLetter(LETTER_GRADE);
}
/**
* Print out the gradebook for all students
* @param students - gradebook
* @param count - number of students to print
*/
void printGrades(struct Student students[], int count) {
} // end printGrades
I need to finish the functions getLetters() and printGrades() and make the program work as shown in the picture
Function "printLetter We only have 2 functions left to build. First a helper method that will give us a character to represent a letter grade, so we can write it out to the console. Enums are great in that their labels are typically easy to read words, but unfortunately, they are just numbers under the covers, so we can't print the identifiers to the console or files. So, we typically end up with a function that you are going to build here. You write function "printletter() and it needs to have a LETTER_GRADE parameter that needs an identifier, and the function should return a "char The function should be a simple Switch statement that looks at the LETTER_GRADE passed in and returns the correct upper-case character, i.e. 'A', "B', etc. You can use the default for 'F' Add the function code somewhere below "main()", a function declaration above "main()", and make sure you provide a function header comment block. Function "printGrades0" This is the final function for the program. This function will output the gradebook for all students and all exam scores. It will show the student's name, each exam score and letter, and their average score aned letter Here is some sample output. uto Gradebook: (1) Enter students (2) Upload grades (3) Print grades (e) Exit nter grades for each student number (x to quit) Student name (e): Tom Student name (1): Dick Student name (2): Harray Student name (3): Xx Auto Gradebook: (1) Enter students (2) Upload grades (3) Print grades (e) Exit s scores uploaded The program starts and the user chooses to enter student names. They enter 3 students and then an They then choose to upload scores for those students and 15 scores are uploadedExplanation / Answer
If you have any doubts, please give me comment...
int getStudents(struct Student students[]) {
int stuCount = 0;
string nameIn = "";
cout << "Enter grades for each student number (X to quit) ";
bool input = false;
while (input == false) {
cout << " Student name (" << stuCount << "):";
getline(cin >>ws, nameIn); // inputting names with whitespace
if ((nameIn == "X") || (nameIn == "x") || (stuCount == STUDENTS_MAX))
input = true;
else{
stuCount++;
students[stuCount - 1].stuName = nameIn;
}
}
return stuCount;
}
char printLetter(float score) {
LETTER_GRADE g = getLetter(score);
char grade;
switch(g){
case D:
grade = 'D';
break;
case C:
grade = 'C';
break;
case B:
grade = 'B';
break;
case A:
grade = 'A';
break;
default:
grade = 'F';
}
return grade;
}
/**
* Print out the gradebook for all students
*
*
*/
void printGrades(Student students[], int count) {
for(int i=0; i<count; i++){
cout<<"Student Name: "<<students[i].stuName<<endl;
double avg = 0.0;
for(int j=0; j<SCORES_MAX; j++){
cout<<"Score "<<(j+1)<<": "<<students[i].grades[j].score<<" "<<printLetter(students[i].grades[j].letter)<<endl;
avg += students[i].grades[j].score;
}
avg /= SCORES_MAX;
cout<<"Average Score: "<<avg<<endl;
cout<<"Grade Letter: "<<printLetter(avg)<<endl;
cout<<endl;
}
}