My program seems correct, but is miscalculating simple arithmetic by exactly 1
Jan 15, 2014 at 3:10pm UTC
I do not get any errors, but the grade calculated is 1 more than it should be. For example, if p1score=95
p2score=87
p3score=86
p4score=75
p5score=83
p6score=72
exam1score=80
exam2score=75
the grade calculated should be 79.52 but it's giving me 80.52 instead. Help please? I really do not know what the problem is.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
#include <iostream> // include standard I/O libraries
#include <iomanip>
using namespace std;
int main()
{
// the constant weights for all scoring items
const double P1_WEIGHT = 0.02;
const double P2_WEIGHT = 0.10;
const double P3_TO_P6_WEIGHT = 0.12;
const double EXAM_WEIGHT = 0.20;
int p1score, // scores on the 6 programs
p2score,
p3score,
p4score,
p5score,
p6score,
exam1score, // scores on the 2 exams
exam2score;
double wtdTotalGrd; // the weighted total grade as calculated
// ask the user to type in the program grades first
cout << "Please enter your score on program 1 -> " ;
cin >> p1score;
cout << " program 2 -> " ;
cin >> p2score;
cout << " program 3 -> " ;
cin >> p3score;
cout << " program 4 -> " ;
cin >> p4score;
cout << " program 5 -> " ;
cin >> p5score;
cout << " program 6 -> " ;
cin >> p6score;
// now ask the user to type in the exam scores
cout << endl << "Please enter your score on exam 1 -> " ;
cin >> exam1score;
cout << " exam 2 -> " ;
cin >> exam2score;
// echoprint all of the user's scores to verify accuracy
cout << endl << "You have entered the following scores:" << endl
<< "\tProgram 1: " << p1score << endl
<< "\tProgram 2: " << p2score << endl
<< "\tProgram 3: " << p3score << endl
<< "\tProgram 4: " << p4score << endl
<< "\tProgram 5: " << p5score << endl
<< "\tProgram 6: " << p6score << endl
<< "\tExam 1: " << exam1score << endl
<< "\tExam 2: " << exam2score << endl << endl;
// calculate the weighted total grade as a real number
wtdTotalGrd = (p1score * P1_WEIGHT) + (p2score * P2_WEIGHT)
+ ((p3score + p4score + p5score + p6score) * P3_TO_P6_WEIGHT)
+ ((exam1score + exam1score) * EXAM_WEIGHT);
// now print the weighted total with 2 digits past the decimal point
cout << fixed << showpoint << setprecision(2);
cout << "Your weighted total is: " << wtdTotalGrd << endl;
Jan 15, 2014 at 3:21pm UTC
On line 57 you mention exam1score twice.
Topic archived. No new replies allowed.