C++ calculation and character issue

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • DossarLX ODI
    Batch Manager
    Game Manager
    FFR Simfile Author
    • Mar 2008
    • 14989

    #1

    C++ calculation and character issue

    I just started getting into C++; this is a simple assignment but there's something about calculations and even a character issue here that I can't figure out.

    The catch of the assignment is to input quiz grades for a student, input the weight the quiz has on the total grade, calculate the student's final grade by getting the average, and then assigning a letter grade (this is the character part).


    Code:
    class Student
    {
        
    public:           
        void input();
        void output();	
    
    private:
        int qgrade[4]; // Each array cell indicates the quiz number's grade.
        int qweight[4]; // Each array cell indicates the quiz number's weight.
        // 0 for quiz1, 1 for quiz2, 2 for midterm, 3 for final exam.
        double avweight;
        char lettergrade;
    
        double calc_ave();
        char calc_letter();
    
    };


    My input function also calls the calc_ave and calc_letter functions to assign a letter grade and final average for that student. The inputs themselves work, but then I have these lines of code and this is when things get screwy.

    Student::avweight = calc_ave();
    Student::lettergrade = calc_letter();

    avweight comes out to be something stupid like 2800 or something, and then lettergrade just doesn't appear at all (it's a blank space where there should be a letter). I'll show the functions so I can make any appropriate corrections concerning what may have gone wrong.


    Code:
    double Student::calc_ave(){
        
        int i;
        double total = 0.0;
        for ( i = 0 ; i < 4 ; i++ )
        {
            total += (double)( Student::qgrade[i] * Student::qweight[i] );
        }
        return total;
    }



    Code:
    char Student::calc_letter(){
        double avgrade = Student::avweight;
        char lgrade; // For storing the letter grade to return
        if( avgrade < 65.0 )
            lgrade = 'F';
        else if( avgrade >= 65.0 && avgrade < 70.0 )
            lgrade = 'D';
        else if( avgrade >= 70.0 && avgrade < 80.0 )
            lgrade = 'C';
        else if( avgrade >= 80.0 && avgrade < 90.0 )
            lgrade = 'B';
        else if( avgrade >= 90.0 && avgrade <= 100.0 )
            lgrade = 'A';
        return lgrade;
    }


    So basically the inputs for the student work, but when I call the calc_ave and calc_letter functions, I print out the values and the letter grade comes up as a blank space while calc_ave gives some number that is way off.

    EDIT: Here's sample output to show what I'm talking about

    Code:
    Please input student 1's grades.
    Grades and weight should be input out of 100.
    Input grade for quiz #1: 80
    Input weight for quiz #1: 20
    You entered 80 and 20
    Input grade for quiz #2: 70
    Input weight for quiz #2: 20
    You entered 70 and 20
    Input grade for midterm exam: 60
    Input weight for midterm exam: 25
    You entered 60 and 25
    Input grade for final exam: 75
    Input weight for final exam: 35
    You entered 75 and 35
    7125Average weight.
      letter grade.
    Student 1's grades successfully entered.
    
    Student Grade Evaluator
    This program enters grades for 3 students and calculates them.
    0 - Quit Program
    1 - Input Grades
    2 - Output Grades
    Please select an item from the menu list: 2
    Student 1's grades are:
    Quiz 1: 80
    Quiz 2: 70
    Miderm Exam: 60
    Final Exam: 75
    This student scored 7125 and got a  for the course.
    80 x .2
    70 x .2
    60 x .25
    75 x .35

    16 + 14 + 15 + 26.25 = 71.25

    It should say the student got a grade of 71.25 and a letter grade of C, but that's not what's happening here.
    Last edited by DossarLX ODI; 01-30-2014, 09:20 AM.
    Originally posted by hi19hi19
    oh boy, it's STIFF, I'll stretch before I sit down at the computer so not I'm not as STIFF next time I step a file
  • arcnmx
    nanodesu~
    • Jan 2013
    • 503

    #2
    Re: C++ calculation and character issue

    Originally posted by DossarLX ODI
    Input grade for quiz #1: 80
    Input weight for quiz #1: 20
    total += (double)( Student::qgrade[i] * Student::qweight[i] );
    Your types are integers, so that would be doing 80*20 rather than 80*0.2

    Your letter isn't appearing since the grade is over 100, and you don't assign anything to lgrade in that case (so it'll default to junk / a meaningless character).
    Last edited by arcnmx; 01-30-2014, 09:28 AM.


    FMO AAAs (1): Within Life :: FGO AAAs (1): Einstein-Rosen Bridge

    Comment

    • DossarLX ODI
      Batch Manager
      Game Manager
      FFR Simfile Author
      • Mar 2008
      • 14989

      #3
      Re: C++ calculation and character issue

      Ok, so I asked the weights to be entered as integers. I forgot to divide by 100.0 when multiplying them in the calc_ave function.

      Since the numbers were too large, it went out of the boundaries of the calc_letter numbers. Thanks arcnmx! I'll test it out again.

      Edit: That works. I thought I somehow didn't get the character assignment syntax correct or something. I was getting junk values before I assigned sizes to the arrays as well.
      Last edited by DossarLX ODI; 01-30-2014, 09:33 AM.
      Originally posted by hi19hi19
      oh boy, it's STIFF, I'll stretch before I sit down at the computer so not I'm not as STIFF next time I step a file

      Comment

      Working...