#include <iostream> #include <string> #include <iomanip> using namespace std; int main() { string ssnumber; int exam1 = 0, exam2 = 0, exam3 = 0, final = 0,count = 0; int average1 = 0, average2 = 0, average3 = 0; double averagegrade, classaverage; while (count < 3) { cout <<"SS Number"; cin >>ssnumber; cout <<"Exam 1"; cin >>exam1; cout <<"Exam 2"; cin >>exam2; cout <<"Exam 3"; cin >>exam3; cout <<"Final"; cin >>final; averagegrade = ( exam1 + exam2+ exam3 + final) * 2 / 5; cout <<"The Average"; cin >>averagegrade; classaverage = (average1 + average2 + average3) / 3; cout<<"Class average is:"; cin >>classaverage; count++; cout<<setw(6)<<ssnumber<<setw(19)<<exam1<<setw(6)<<exam2<<setw(6) <<exam3<<setw(6)<<final<<setw(6)<<average1<<setw(6)<<average2<<setw(12) <<average3<<setw(6)<<averagegrade<<setw(6)<<classaverage<<endl; } return 0; }
Student Grade Average
Page 1 of 15 Replies - 1331 Views - Last Post: 01 September 2013 - 01:49 PM
#1
Student Grade Average
Posted 01 September 2013 - 06:06 AM
It doesn't seem to be calculating the average grade, I don't see any error in my code. Any help will be very much appreciated.
Replies To: Student Grade Average
#2
Re: Student Grade Average
Posted 01 September 2013 - 06:16 AM
Your program is dividing integers, to create your averages.
Question: what is int 3/ int 2?
Now how should you be doing this division?
Question: what is int 3/ int 2?
Now how should you be doing this division?
#3
Re: Student Grade Average
Posted 01 September 2013 - 06:21 AM
So it should be double right? Well, I used 3 to divide the three averages.
Thank you for your help!
Thank you for your help!
#4
Re: Student Grade Average
Posted 01 September 2013 - 09:27 AM
It should be important to remember this in C++:
When storing divided ints into a float or double, it will store a truncated int, NOT a float.
ie:
Solution:
Cast either(or both) the numerator or denominator as double.
So simply casting the denominator in your case will be sufficient not to truncate and lose those valuable decimal points:
When storing divided ints into a float or double, it will store a truncated int, NOT a float.
ie:
float result = 1/2;//your probably think it will store 0.5, but no, it will truncate to 0.
Solution:
Cast either(or both) the numerator or denominator as double.
So simply casting the denominator in your case will be sufficient not to truncate and lose those valuable decimal points:
averagegrade = ((exam1 + exam2 + exam3 + final) * 2) / double(5)
#5
Re: Student Grade Average
Posted 01 September 2013 - 10:27 AM
Since you're using C++, IMO, you should learn the newer C++ style casts and not the older C-style casts which are less type-safe. Here's a link about both styles of casting. In this situation you could just use the static_cast operator:
I know it's longer than the C-style casts, but you'll be better off learning the C++ style casts.
double num = 1 / static_cast<double>(2);
I know it's longer than the C-style casts, but you'll be better off learning the C++ style casts.
This post has been edited by vividexstance: 01 September 2013 - 05:52 PM
#6
Re: Student Grade Average
Posted 01 September 2013 - 01:49 PM
Thank you so much guys!
Page 1 of 1