-
prob function call
Hi everyone...
I am working on a function that calculates several diff parts of student grades (Student is my stuct and has 6 parts of each students info IE: name, grades) I am trying to calculated the AVERAGe of those grades, transform them into a letter grade and return both & a status of 'passed' or 'failed'...my prob is with the code below...i think it is in my call for the Calcav and / or CalGrade.
I am getting an access violation and the exe crashes on this.
also i am not returning the average or grade from other functions that work on their own -assuming that the prototypes are correct
can anyone see a problem with the highlighted lines or anywhere else???? thanks a bunch all, this forum has taught me more than my class has!!
Mouse
[code]
//Function Calculate=====================
void Calculate(Student CL[], int ns){
for ( int x =0; x,ns; x++){
CL[x].average = Calcav(CL[x].exams, 4);
CL[x].letgrade = CalcGrade(CL[x].average);
//CL[x].passed = status(CL[x].letgrade);
}
cout << CL[x].average << endl;
cout << CL[x].letgrade << endl;
}
//Function Calcave=====================
double Calcav(double ex[], int nex){
double Sum = 0;
double AV = 0;
nex = 4;
for (int x = 0; x < nex; x++){
Sum+= ex[x];
AV = Sum/nex;
}
return AV;
}
//Function CalcGrade=====================
char CalcGrade(double AV){
{
if(AV >= 90) return 'A';
if(AV >= 80) return 'B';
if(AV >= 70) return 'C';
if(AV >= 60) return 'D';
return 'F';
}
}
-
Code:
for ( int x =0; x,ns; x++){
I doubt you want a comma there between the x and ns, perhaps you meant "<"? If you did, that would certainly explain your access violation because it will continue the loop indefinately and eventually x will be greater than the array size.
-
you really only need to calculate AV once, after all the exam results have been tallied. Doesn't hurt to calculate it everytime a new grade is added in, but doesn't have to.
The calculation of average and determination of grade functions look fine. However you over read the array in Calculate function when you attempt to display the results, work through the function to determine what the value of x is when you call cout to see why this is so.
I think you will be able to pick up the error better if you change the style of your code. By that I mean indent better and don't use the style where the opening curly brace is at the end of the line, put it on a line all by itself where you can keep track of it. If you do this, I think you'll see the logic error in the Calculate function too, and then you should be all set.
-
thank you
for your help and advice...i feel like a doink missing that comma!
i am going to try to go over my syntax with the brackets re-set to see if i can see what you're referring to by 'overreading them
mouse
-
Hint: where are the cout statements relative to the for loop and what is the value of x at that point and what does that value of x do when used as an index of the array?