Thread: prob function call

  1. #1
    Registered User mouse163's Avatar
    Join Date
    Dec 2002
    Posts
    49

    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';

    }


    }

  2. #2
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    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.

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    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.

  4. #4
    Registered User mouse163's Avatar
    Join Date
    Dec 2002
    Posts
    49

    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

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    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?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  2. Troubleshooting Input Function
    By SiliconHobo in forum C Programming
    Replies: 14
    Last Post: 12-05-2007, 07:18 AM
  3. temperature sensors
    By danko in forum C Programming
    Replies: 22
    Last Post: 07-10-2007, 07:26 PM
  4. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  5. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM