Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,154,195 members, 7,822,042 topics. Date: Thursday, 09 May 2024 at 03:39 AM

Help On This Code - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / Help On This Code (1274 Views)

Please,Help Explain This Code In C / Java Code Pls Modify This Code Help / Java Programmers ,I Need Help Fixing This Code. (2) (3) (4)

(1) (Reply) (Go Down)

Help On This Code by Justiceotuya(m): 11:35pm On Jan 10, 2017
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 course, I have tried what I can do but still can't detect the bug. I need help before I run mad (lol) cc dhtml and others


#include<iomanip>
#include <iostream>
using namespace 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";
} else if (scores[i] >=40 && scores[i] <= 44){
gradePoint[i]=1;
letterGrade[i] ="E";
} else if (scores[i] >=45 &&scores[i]<= 49){
gradePoint[i]=2;
letterGrade[i] = "grin";
}else if (scores[i] >=50 &&scores[i] <= 59){
gradePoint[i] =3;
letterGrade[i] = "C";
}else if (scores[i] >=60 &&scores[i] <= 69){
gradePoint[i]=4;
letterGrade[i] = "B";
}else if (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;

}

Re: Help On This Code by DangotePikin: 11:25am On Jan 12, 2017
Is it an ArrayOutOfBound exception?
Re: Help On This Code by peterincredible: 12:23pm On Jan 12, 2017
op the fault is 4rm the ( gradepoint and lettergrade which have a pridefined index of 6) unlike the (score,creditunit and qualitypoint which their index is determined by the variable(numofcode) so to me to correct the bug make the number of index in the gradepoint and lettergrade array be determined by the numofcourse variable i mean in ure code change the int gradepoint[6],lettergrade[6] to intgradepoint[numofcode] and int lettergrade[numofcode] i think with this ure problem is solved tank u very much
Re: Help On This Code by peterincredible: 12:29pm On Jan 12, 2017
and i forgot if u remember the for loop use numofcourse variable to check the amount of loop it will execute but the the gradepoint and lettergrade array has a fixed index of 6 so when you now make numofcourse variable 7 or higher number it will make gradepoint and lettergrade throw an exception tanks
Re: Help On This Code by elfico(m): 1:56pm On Jan 12, 2017
@OP, first of all, what Compiler or IDE are you using?

Because as I understand, what you doing currently is illegal in C++ except of course you are using C99 (a version of C that could be used as an extension in C++)

You cannot declare an array without a constant value at compile time. That is,

int numOfCourse;
int scores[numOfCourse]; //illegal.

You can solve this by using a dynamic array i.e, by using new and delete operators

a. int numOfCourse;
cin >> numOfCourse
int *scores = new int[numOfCourse];
//all other codes here
delete[] scores //very important

b.
int numOfCourse = 5; // declare the size of the vector
vector<int> scores(size, 0); // create a vector to hold "size" ints all initialized to zero
scores[0] = 1234; // assign values like a c++ array
Re: Help On This Code by Justiceotuya(m): 7:32pm On Jan 14, 2017
elfico:
@OP, first of all, what Compiler or IDE are you using?

Because as I understand, what you doing currently is illegal in C++ except of course you are using C99 (a version of C that could be used as an extension in C++)

You cannot declare an array without a constant value at compile time. That is,

int numOfCourse;
int scores[numOfCourse]; //illegal.

You can solve this by using a dynamic array i.e, by using new and delete operators

a. int numOfCourse;
cin >> numOfCourse
int *scores = new int[numOfCourse];
//all other codes here
delete[] scores //very important

b.
int numOfCourse = 5; // declare the size of the vector
vector<int> scores(size, 0); // create a vector to hold "size" ints all initialized to zero
scores[0] = 1234; // assign values like a c++ array




Thanks, I am using DEV c++ and it actually compiles. Thanks for the Tip.the problem is that letter grade and grade point must be predefined to 6 to hold 0-5 and A-F
Re: Help On This Code by asalimpo(m): 1:42am On Jan 15, 2017
use codeblocks please. Dev-C++ is bad.

(1) (Reply)

Why I Love Reading Other People’s Code And You Should Too / Why Doesn't This Work On Nairaland? / Just Launched My Android Game On Google Play

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 22
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.