5 Replies - 579 Views - Last Post: 01 September 2013 - 01:49 PM Rate Topic: -----

#1 xneverletgo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 01-September 13

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.
#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;

}



Is This A Good Question/Topic? 0
  • +

Replies To: Student Grade Average

#2 Adak  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 331
  • View blog
  • Posts: 1,168
  • Joined: 01-April 11

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?
Was This Post Helpful? 1
  • +
  • -

#3 xneverletgo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 01-September 13

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!
Was This Post Helpful? 0
  • +
  • -

#4 Java Student  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 21
  • View blog
  • Posts: 480
  • Joined: 05-February 10

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:
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)


Was This Post Helpful? 1
  • +
  • -

#5 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 651
  • View blog
  • Posts: 2,225
  • Joined: 31-December 10

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:
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

Was This Post Helpful? 1
  • +
  • -

#6 xneverletgo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 01-September 13

Re: Student Grade Average

Posted 01 September 2013 - 01:49 PM

Thank you so much guys!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1