Student Grade Average

Page 1 of 1

5 Replies - 958 Views - Last Post: 01 September 2013 - 01:49 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=328065&amp;s=eea5e88fb2a2fdd3ba71c86ef78cb268&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 xneverletgo

• New D.I.C Head

Reputation: 0
• 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

• D.I.C Lover

Reputation: 331
• 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

• New D.I.C Head

Reputation: 0
• 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

• D.I.C Regular

Reputation: 21
• Posts: 496
• 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

• D.I.C Lover

Reputation: 763
• Posts: 2,679
• 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

• New D.I.C Head

Reputation: 0
• 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

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }