Thread: Program seems correct, but it's miscalculating??

  1. #1
    Registered User
    Join Date
    Jan 2014
    Posts
    1

    Program seems correct, but it's miscalculating??

    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.


    Code:
    #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;
    Last edited by cheer4BAS; 01-15-2014 at 09:09 AM.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    You need to read this link to learn about how to post code in this forum readably.


    The code as you have posted it would not compile, let alone produce any output - correct or not.

    The offending lines are the three you posted immediately after the comment "// calculate the weighted total grade as a real number".

    The assignment is invalid (and will cause a compiler to reject your code).

    You've also left off a closing curly brace, but I assume that is just a copy/paste problem.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Jan 2014
    Posts
    1
    Quote Originally Posted by cheer4BAS View Post
    // 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);

    Beyond what the person above me said, Look at the bold above.
    The miscalculation is a typo of yours.
    Instead of exam1score + exam2score, you added the exam1score twice.

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Also posted here.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to Correct the search In the end of the program
    By rajatahirqaiser in forum C++ Programming
    Replies: 1
    Last Post: 06-01-2013, 04:01 PM
  2. please help me correct this program
    By loogan211 in forum C Programming
    Replies: 5
    Last Post: 02-09-2013, 11:53 PM
  3. correct this program anyone please....
    By jothimani1991 in forum C Programming
    Replies: 3
    Last Post: 01-08-2013, 06:49 PM
  4. another little program: is this correct?
    By fsx in forum C Programming
    Replies: 7
    Last Post: 05-19-2009, 07:50 AM
  5. Is this program correct ?
    By broli86 in forum C Programming
    Replies: 9
    Last Post: 08-02-2008, 06:01 PM