Skip to content

Instantly share code, notes, and snippets.

@Parables
Last active April 23, 2020 07:34
Show Gist options
  • Select an option

  • Save Parables/7c9090bbde61341fb76b420f1ee3bba8 to your computer and use it in GitHub Desktop.

Select an option

Save Parables/7c9090bbde61341fb76b420f1ee3bba8 to your computer and use it in GitHub Desktop.
C++ GPA Result Generator
// Example program
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
// Data Structures
typedef struct{
string title;
int creditHours;
} Course;
typedef struct{
string title;
double marks;
string grade;
int creditHours;
double wgp;
} Result;
typedef struct{
string name;
Result results[10000]; // This is just a hack but it should be equal to the noCourses
} Student;
typedef struct{
string grade;
double gradePoint;
} Grade;
// Global variables
int noStudents, noCourses = 0;
Grade gradeTable[9];
// Function prototypes
int getGrade(double marks){
if(marks >= 80.0 && marks<= 100 ) return 0;
else if(marks >= 75.0 && marks<= 79.9) return 1;
else if(marks >= 70.0 && marks<= 74.9) return 2;
else if(marks >= 65.0 && marks<= 69.9) return 3;
else if(marks >= 60.0 && marks<= 64.9) return 4;
else if(marks >= 55.0 && marks<= 59.9) return 5;
else if(marks >= 50.0 && marks<= 54.9) return 6;
else if(marks >= 45.0 && marks<= 49.9) return 7;
else if(marks >= 0 && marks<= 44.9) return 8;
else return -1;
}
double calcWGP(int creditHours, int grade){
return creditHours * gradeTable[grade].gradePoint;
}
int main()
{
// initialise gradeTable
gradeTable[0]= {"A", 4.00};
gradeTable[1]= {"B+", 3.50};
gradeTable[2]= {"B", 3.25};
gradeTable[3]= {"B-", 3.00};
gradeTable[4]= {"C+", 2.50};
gradeTable[5]= {"C", 2.25};
gradeTable[6]= {"C-", 2.00};
gradeTable[7]= {"D", 1.00};
gradeTable[8]= {"F", 0.00};
cout<<"Welcome to GPA Calculator\n\n|Let's start by adding some courses...\n|"<<endl;
cout<< "|--> How many courses do you want to create? "; cin>> noCourses;
// Create a CourseList Array
Course coursesList[noCourses];
// Add courses to the List
cout<<"|Please enter the names and credit hours for the "<<noCourses<< " Courses...\n|Press enter after each entry..."<<endl; cin.ignore();
// temp course variable
Course course;
for(int i=0; i<noCourses; i++) {
cout<<"|<-- Course "<<i+1<<" title: "; getline(cin,course.title);
cout<<"|<-- Course "<<i+1<<" credit hours: "; cin>>course.creditHours; cout<<"|\n"; cin.ignore();
coursesList[i] = course;
}
cout<< "|--> How many students do you want to register? "; cin>> noStudents;
// Create a StudentList Array
Student studentsList[noStudents];
// Add students to the List
cout<<"|Please enter the names of the "<<noStudents<< " Students...\n|Press enter after each entry..."<<endl; cin.ignore();
// temp student variable
Student student;
for(int i=0; i<noStudents; i++) {
cout<<"|<-- Student "<<i+1<<" name: "; getline(cin,student.name);
studentsList[i] = student;
}
cout<<"|\n|Geat!, now input the results for the student... "<<endl;
for(int s=0; s<noStudents; s++){
cout<<"|--> Enter the results for "<<studentsList[s].name<<endl;
// temp variable for marks entered
double marks, wgp; int gradeIndex;
Result result;
for(int c=0; c<noCourses; c++){
cout<<"|<--Enter the marks for "<<coursesList[c].title<<": "; cin>>marks;
// Add to student's results
gradeIndex = getGrade(marks);
wgp = calcWGP(coursesList[c].creditHours, gradeIndex);
result.title= coursesList[c].title;
result.marks= marks;
result.creditHours= coursesList[c].creditHours;
result.grade = gradeTable[gradeIndex].grade;
result.wgp= wgp;
studentsList[s].results[c] = result;
};
cout<<"|\n";
}
//Print Results
cout<<"\n\nSECOND SEMESTER RESULTS\n**************";
for (int s=0; s< noStudents; s++){
cout<<"\n\nStudent Name: "<<studentsList[s].name<<endl;
cout<<setw(30) << left<< "Course Title"<<setw(8) << left <<"Mark"<<setw(8) << left <<"Grade"<<setw(8) << left<<"CHrs"<<setw(8) << left <<"WGP\n************************************************************"<<endl;
for(int r=0; r<noCourses; r++){
string title = studentsList[s].results[r].title;
double marks = studentsList[s].results[r].marks;
string grade = studentsList[s].results[r].grade;
int creditHours = studentsList[s].results[r].creditHours;
double wgp = studentsList[s].results[r].wgp;
cout << setw(30) << left <<title<< setw(8) << left <<marks<<setw(8) << left <<grade<<setw(8) << left <<creditHours<<setw(8) << left <<wgp<<endl;
}
cout<<"************************************************************";
} }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment