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.
#include <iostream> //header
#include <string>
#include <cmath>
#include <cstdlib>
usingnamespace 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
elseif (avgs.final_grade>=50,avgs.final_grade<=60) cout<<"The Final Grade is a C-"<<endl; //from 59 to 60 is C-
elseif (avgs.final_grade>=61,avgs.final_grade<=69) cout<<"The Final Grade is a C"<<endl; //from 60 to 69 is C
elseif (avgs.final_grade>=70,avgs.final_grade<=75) cout<<"The Final Grade is a C+"<<endl; //from 70 to 75 is C+
elseif (avgs.final_grade>=76,avgs.final_grade<=84) cout<<"The Final Grade is a B"<<endl; //from 76 to 84 is B
elseif (avgs.final_grade>=85,avgs.final_grade<=89) cout<<"The Final Grade is a B+"<<endl; //from 85 to 89 is B+
elseif (avgs.final_grade>=90,avgs.final_grade<=94) cout<<"The Final Grade is a A"<<endl; //from 90 to 94 is A
elseif (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;
}
}
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.
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.