Skip to content

Instantly share code, notes, and snippets.

@TuxPenguin09
Last active March 31, 2024 13:59
Show Gist options
  • Save TuxPenguin09/531da62870fd2ee80156bcc6914c7107 to your computer and use it in GitHub Desktop.
Save TuxPenguin09/531da62870fd2ee80156bcc6914c7107 to your computer and use it in GitHub Desktop.
Hexagon School Grading System Code 1
#include <iostream>
#include <cstring>
#include <cstdlib>
using namespace std;
// Revision 6 by SRD_67
const int MAX_TOTAL_STUDENTS = 20; // Maximum of Students in Total
const int MAX_TOTAL_SUBJECTS = 10; // Maximum of Subjects in Total
void purge(char* students[], int stud_count) {
for (int i = 0; i < stud_count; ++i)
{
delete[] students[i]; // Purge all Students in Memory
}
}
string failmessage() {
// A message to student that fails
string messages[] = {
"My son so dumb IQ 21 He try to make friends but they call him ching chong",
"You are a failure",
"You are a disgrace to your asian family",
"I have no dumb child",
"Your friends are smarter than you"
};
int randomIndex = rand() % (sizeof(messages) / sizeof(messages[0]));
return messages[randomIndex];
}
float calculateGPA(float score) {
// Hexagon School based Final Grades
float gpa;
if (score >= 98.0 && score <= 100.0) {
gpa = 1.00;
} else if (score >= 95.0 && score <= 97.0) {
gpa = 1.25;
} else if (score >= 92.0 && score <= 94.0) {
gpa = 1.50;
} else if (score >= 89.0 && score <= 91.0) {
gpa = 1.75;
} else if (score >= 86.0 && score <= 88.0) {
gpa = 2.00;
} else if (score >= 83.0 && score <= 85.0) {
gpa = 2.25;
} else if (score >= 80.0 && score <= 82.0) {
gpa = 2.50;
} else if (score >= 77.0 && score <= 79.0) {
gpa = 2.75;
} else if (score >= 75.0 && score <= 76.0) {
gpa = 3.00;
} else {
gpa = 5.00;
}
return gpa;
}
int main() {
char* students[MAX_TOTAL_STUDENTS];
int stud_count = 0;
char stud_name[100]; // Max of 100 characters in Student's name
char* subjects[MAX_TOTAL_SUBJECTS];
int sub_count = 0;
char sub_name[100]; // Max of 100 characters in Subject's name
char choice;
// Start of student/subject input loop
do {
stud_count = 0; // Reset student count
sub_count = 0; // Reset subject count
cout << "Enter student names. Type 'f' to finish.\n";
do {
cout << stud_count + 1 << ": ";
cin.getline(stud_name, 100); // Input student name
if (strcasecmp(stud_name, "f") == 0) {
break;
}
bool isValidInput = false;
for (int i = 0; stud_name[i] != '\0'; ++i) {
if (!isspace(stud_name[i])) {
isValidInput = true;
break;
}
}
if (isValidInput) {
students[stud_count] = new char[strlen(stud_name) + 1];
strcpy(students[stud_count], stud_name);
stud_count++;
} else {
cout << "Invalid input. Please enter a non-blank/non-whitespace name." << endl;
}
} while (stud_count < MAX_TOTAL_STUDENTS);
cout << "\nList of students:\n";
for (int i = 0; i < stud_count; ++i)
{
cout << i + 1 << ". " << students[i] << endl;
}
cout << "\nEnter subjects. Type 'f' to finish.\n";
do {
cout << sub_count + 1 << ": ";
cin.getline(sub_name, 100); // Input subject name
if (strcasecmp(sub_name, "f") == 0) {
break;
}
bool isValidInput = false;
for (int i = 0; sub_name[i] != '\0'; ++i) {
if (!isspace(sub_name[i])) {
isValidInput = true;
break;
}
}
if (isValidInput) {
subjects[sub_count] = new char[strlen(sub_name) + 1];
strcpy(subjects[sub_count], sub_name);
sub_count++;
} else {
cout << "Invalid input. Please enter a non-blank/non-whitespace name." << endl;
}
} while (sub_count < MAX_TOTAL_SUBJECTS);
cout << "\nList of subjects:\n";
for (int i = 0; i < sub_count; ++i)
{
cout << i + 1 << ". " << subjects[i] << endl;
}
cout << "Are you sure this is correct? [Only the first character is noted as your answer]\n"; // Prompt user if everything students and subjects are correct
cout << "Press 'y' to continue or 'n' to start over.\n";
choice = cin.get();
cin.ignore(256, '\n');
while (choice != 'n' && choice != 'y' && choice != 'N' && choice != 'Y') {
cout << "Invalid choice. Please enter 'y' or 'n' [Only the first character is noted as your answer]." << endl;
choice = cin.get();
cin.ignore(256, '\n');
}
if (choice == 'n' || choice == 'N') {
purge(students, stud_count);
purge(subjects, sub_count);
cin.clear();
cin.ignore();
}
} while (choice == 'n' || choice == 'N');
for (int i = 0; i < stud_count; ++i) {
cout << "Enter scores for " << students[i] << endl;
for (int j = 0; j < sub_count; ++j) {
float score;
cout << subjects[j] << ": ";
cin >> score;
// Input validation
while (score < 0 || score > 100) {
cout << "Invalid score. Please enter a score between 0 and 100." << endl;
cin.clear(); // Clear the error state
cout << subjects[j] << ": ";
cin >> score;
}
cout << "GPA: " << calculateGPA(score) << endl;
if (calculateGPA(score) == 5.00) {
cout << failmessage() << endl;
}
}
}
// Deallocate memory
purge(students, stud_count);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment