### Average Calculation function not working as expected to

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.

 ``1234567891011121314151617`` ``````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 `int`s. 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:
 ``1234`` ``````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`.
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
Modified whole code:

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159`` ``````#include //header #include #include #include 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<>flag; if (flag=='y') { system("CLS"); } else { system("CLS"); display_grades(avgs); cout<> avgs.student; cout<<" Course: "; cin>>avgs.course; cout<>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"<=50,avgs.final_grade<=60) cout<<"The Final Grade is a C-"<=61,avgs.final_grade<=69) cout<<"The Final Grade is a C"<=70,avgs.final_grade<=75) cout<<"The Final Grade is a C+"<=76,avgs.final_grade<=84) cout<<"The Final Grade is a B"<=85,avgs.final_grade<=89) cout<<"The Final Grade is a B+"<=90,avgs.final_grade<=94) cout<<"The Final Grade is a A"<=95,avgs.final_grade<=100)cout<<"The Final Grade is a A+"<>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
oh sorry i took that out.
... Did you try my advice?
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.
Well, thank you for your corporation. I'll see what i can do.
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.