Hello, programmers, I am a noob in programming and was told that to learn more, I need to do some project and so I started this GPA calculator that eventually prints your GPS in both 5unit and it 4 unit equivalent. The problem is I created an array to hold the grade point from A-F and another array to hold the letter grade from 0-5, but after I was done it performed well until I noticed it crashes when I try to calculate for more than 6 courses, I have tried what I can do but still can't detect the bug.
#include<iomanip>
#include <iostream>
usingnamespace std;
int main(){
int numOfCourse;
cout<<"insert the number of courses offered: ";
cin>>numOfCourse;
int scores[numOfCourse];//an array that stores the number of course inputed by the user
int creditUnit[numOfCourse];//another array that stores the credit unit of the user
int qualityPoint[numOfCourse];//this is gotten by multiplying the credit unit by grade point
int totalQualityPoint=0;
int totalCreditUnit=0;
int gradePoint[6];//predefined grade point 0-5
string letterGrade[6];//predefined letter grade A-F which corredponds with the grade point
float gradePointAverage5;//5 point GPA Scale
float gradePointAverage4;// 4 point GPA Scale
// Asks the user to input scores and credit load of the courses
for (int i=0; i< numOfCourse; i++){
cout<<"input the scores of the course: ";
cin>>scores[i];
cout<<" input the credit load of the course: ";
cin>>creditUnit[i];
//defining the gradepoint and its corresponding letter grade
if(scores[i] <= 39){
gradePoint[i]=0;
letterGrade[i] = "F";
} elseif (scores[i] >=40 && scores[i] <= 44){
gradePoint[i]=1;
letterGrade[i] ="E";
} elseif (scores[i] >=45 &&scores[i]<= 49){
gradePoint[i]=2;
letterGrade[i] = "grin";
}elseif (scores[i] >=50 &&scores[i] <= 59){
gradePoint[i] =3;
letterGrade[i] = "C";
}elseif (scores[i] >=60 &&scores[i] <= 69){
gradePoint[i]=4;
letterGrade[i] = "B";
}elseif (scores[i]>=69 &&scores[i] <= 100){
gradePoint[i]=5;
letterGrade[i] = "A";
}else{
cout<< "Your grade is incorrect"<<endl;
return 0;
}
}
cout<<"Scores Letter Credit Grade QualityPoint"<<endl;
cout<<" Grade Unit Point (CU * GP)"<<endl;
for(int i =0; i<numOfCourse;i++){
qualityPoint[i] = creditUnit[i] * gradePoint[i];
cout<<scores[i] <<" "<<letterGrade[i]<<" "<<creditUnit[i]
<<" "<<gradePoint[i]<< " "<<qualityPoint[i]<<endl;
totalQualityPoint=totalQualityPoint + qualityPoint[i];
totalCreditUnit=totalCreditUnit + creditUnit[i];
gradePointAverage5= float(totalQualityPoint)/float(totalCreditUnit);
gradePointAverage4 = (gradePointAverage5/5)*4;
}
cout<< "Your total Quality Point is: "<<totalQualityPoint<<endl;
cout<< "Your total Credit Unit is: "<<totalCreditUnit<<endl;
cout<< "Your Grade Point Average (GPA) on the 5 point scale is : "<<setprecision(2)<<fixed<<gradePointAverage5<<endl;
cout<< "Your Grade Point Average (GPA) on the 4 point scale is : "<<setprecision(2)<<fixed<<gradePointAverage4<<endl;
}
gradePoint and letterGrade are arrays of 6 elements each. With numOfCourse equal to 7, for example, it triggers out of bounds access inside the for loop (line 31 and below)