Average Calculation function not working as expected to

closed account (ETA9216C)
Prior to this post: http://www.cplusplus.com/forum/general/120061/

My calculation function will not work like i wanted it to.
It gives me 0 or a crazy number.

this is the function here:
the entire code is on the link.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
double calculations(data&avgs)          //Calculates the students grade into a final grade
{
    int avg1;
    int avg2;
    int avg3;
    int lab1;

        avg1=((avgs.test1/100)*15);
        avg2=((avgs.midterm/100)*25);
        avg3=((avgs.finalexam/100)*30);
        lab1=((avgs.lab/100)*30);

        avgs.final_grade=avg1+avg2+avg3+lab1;


return avgs.final_grade;
}
What data type is data?
But I can look at your code right now, and I see a pretty big flaw. I am going to guess that your data contains ints. If that's the case, your dividing an int by another int. This results in the computer rounding the result down. If any of your integers are below 100, it will result in 0.

The correct code is:
1
2
3
4
avg1=((avgs.test1/100.0f)*15);
avg2=((avgs.midterm/100.0f)*25);
avg3=((avgs.finalexam/100.0f)*30);
lab1=((avgs.lab/100.0f)*30);


Now your dividing an int by a float, which produces a float. Now you're multiplying a float by an int, which produces another float.

And then the computer implicitly converts this float into an int.
closed account (ETA9216C)
my data is a structure. It does contain ints. I'm still kinda of a newbie even after a semester of this stuff. Thank you for the fast response.

But yeah still getting crazy numbers.
Last edited on
Define "crazy number", please.
closed account (ETA9216C)
Modified whole code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#include <iostream>           //header
#include <string>
#include <cmath>
#include <cstdlib>



using namespace std;

struct data
{
string student;
int final_grade;
double lab;
double test1;
double midterm;
double finalexam;
string course;

};

    void getinput(data&);
    void students_avg(data&);
    void system_pause();
    void display_grades(data&);
    void proceed();
    double calculations(data&);              //header

int main()
{
    int final_grade = 0;
    data avgs;
    char flag= 'y';


     while(flag=='y')
        {
        getinput(avgs);
        students_avg(avgs);

              cout<<endl;
              cout<<"Do you have other students? y, yes.Anything else is no";
              cout<<endl;
              cin>>flag;
                if (flag=='y')
                   {
                        system("CLS");
                   }
                else
                    {
                    system("CLS");
                    display_grades(avgs);
                    cout<<endl;
                    students_avg(avgs);
                    cout<<endl;
                    proceed();
                    system_pause();
                   }
             }

    return 0;
}

void getinput(data&avgs)        // display grading policy and user inputs student's grade
{
    cout<<"Grading Policy"<<endl;
    cout<<"15% test 1 ";
    cout<<"25% midterm";
    cout<<"30% final";
    cout<<"30% lab"<<endl<<endl;


        cout<<"  Student's name: ";
        cin>> avgs.student;
        cout<<"  Course: ";
        cin>>avgs.course;
        cout<<endl;


        cout<<"  Enter grade for test 1: ";
        cin>>avgs.test1;
        cout<<"  Enter grade for midterm: ";
        cin>>avgs.midterm;
        cout<<"  Enter Grade for Final exam: ";
        cin>>avgs.finalexam;
        cout<<"  Enter grade for lab ";
        cin>>avgs.lab;
}

double calculations(data&avgs)          //Calculates the students grade into a final grade
{
    double avg1;
    double avg2;
    double avg3;
    double lab1;

        avg1=((avgs.test1/100.0f)*15.0);
        avg2=((avgs.midterm/100.0f)*25.0);
        avg3=((avgs.finalexam/100.0f)*30.0);
        lab1=((avgs.lab/100.0)*30.0f);

        avgs.final_grade=avg1+avg2+avg3+lab1;


return avgs.final_grade;
}

void students_avg(data&avgs)      //gives a letter grade from calculation function
{
    if (avgs.final_grade>=0,avgs.final_grade<=49)  cout<<"The Final Grade is a F"<<endl;            //from 0 to 49 is F
        else if (avgs.final_grade>=50,avgs.final_grade<=60) cout<<"The Final Grade is a C-"<<endl;  //from 59 to 60 is C-
        else if (avgs.final_grade>=61,avgs.final_grade<=69) cout<<"The Final Grade is a C"<<endl;   //from 60 to 69 is C
        else if (avgs.final_grade>=70,avgs.final_grade<=75) cout<<"The Final Grade is a C+"<<endl;  //from 70 to 75 is C+
        else if (avgs.final_grade>=76,avgs.final_grade<=84) cout<<"The Final Grade is a B"<<endl;   //from 76 to 84 is B
        else if (avgs.final_grade>=85,avgs.final_grade<=89) cout<<"The Final Grade is a B+"<<endl;  //from 85 to 89 is B+
        else if (avgs.final_grade>=90,avgs.final_grade<=94) cout<<"The Final Grade is a A"<<endl;   //from 90 to 94 is A
        else if (avgs.final_grade>=95,avgs.final_grade<=100)cout<<"The Final Grade is a A+"<<endl;  //from 95 to 100 is A+
}

void display_grade(data&avgs)       //Displays the students data
{
    cout<<avgs.student;
    cout<<avgs.course;
    cout<<endl;

    cout<<"lab: "<<avgs.lab;
    cout<<"test 1: "<<avgs.test1;
    cout<<"mideterm: "<<avgs.midterm;
    cout<<"final exam: "<<avgs.finalexam;
    cout<<endl;

    cout<<avgs.final_grade;
}

void system_pause() //alternative to system("pause")
{
    cout << endl;
    cin.sync();
    cout << "Press any key...";
    cin.ignore();
}

void proceed()  //this function let's the user to either make the student retake the course or have them pass the course
{
   int x;

    cout<<"What would you like to do?"<<endl;
    cout<<"type 1 or 2"<<endl;
    cout<<"1. Have student to retake course"<<endl;
    cout<<"2.Student may proceed to next course"<<endl<<endl;
    cin>>x;
        switch(x)
        {
         case 1: cout<<"student MUST retake the course due to average not meeting the requirements.";break;
         case 2: cout<<"Student can proceed to the next course.";break;
         default: cout<<"Please type in the correct input";break;

        }
}
Last edited on
I can't see your definition of final_grades() anywhere. Try moving that statement above the statement finding your average.
Last edited on
closed account (ETA9216C)
oh sorry i took that out.
... Did you try my advice?
closed account (ETA9216C)
The final_grades() was not suppose to be there, i just forgot to take it out. or did you mean something else?

Last edited on
Nevermind. Anyway, it's late here, and I am incapable of debugging your code right now. Either someone else will have to do it for you, or I may remember this tomorrow.
closed account (ETA9216C)
Well, thank you for your corporation. I'll see what i can do.
closed account (ETA9216C)
I fixed it. I forgot to call the calculation() function in the main function.
Okay, good to hear. I thought it was related to uninitialized variables, and it seems like that was the case. Except I thought final_grades() would initialize the variables or something, that's why I brought it up earlier.
Topic archived. No new replies allowed.