Function not returning value.

I have an assignment for class to calculate my grade so far using functions. All of it works fine except for the function that calculates my quiz average. It just returns zero no matter what. Here's the relevant code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
double calculateQuizScore (double, double, double, double);
int main ()
{
    double name, Q1, Q2, Q3, Q4;
    cout << "Enter 4 quiz grades here, with spaces between the grades: ";
    cin >> Q1 >> Q2 >> Q3 >> Q4;
    double quizTotal=calculateQuizScore (Q1, Q2, Q3, Q4);
    cout << quizTotal << endl;
}

double calculateQuizScore (double Q1, double Q2, double Q3, double Q4)
{
    Q1=Q1*10/15;
    Q2=Q2*10/100;
    Q3=Q3*10/20;
    Q4=Q4*10/25;
    
    double quizTotal=(Q1+Q2+Q3+Q4)*(25/40);
    return quizTotal;
}


Sorry about the awkwardness of the definitions for Q1, Q2, etc. in the calculateQuizScore function. My professor wanted it that way. The math should all be correct. The function just always returns zero. Thanks for the help!
Last edited on
The C++ compilers are not as sophisticated as the TI calculators so you need to play around with them until you figure how they work. Your equations are throwing me off but use this code to figure out exactly what you want. You may need to multiply them by 10 or 100 to get the answer you need.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
double calculateQuizScore (double Q1, double Q2, double Q3, double Q4);

int main ()
{
    double name, Q1, Q2, Q3, Q4, A1, A2, A3, midTerm;
    cout << "Enter 4 quiz grades here, hitting enter after each grade: ";
    cin >> Q1 >> Q2 >> Q3 >> Q4;
    cout << calculateQuizScore (Q1, Q2, Q3, Q4) << endl;
}

double calculateQuizScore (double Q1, double Q2, double Q3, double Q4)
{
	Q1 = (Q1 * 10) / 15;    
    Q2 = (Q2 * 10) / 100;   
    Q3 = (Q3 * 10) / 20;    
    Q4 = (Q4 * 10) / 25;     

    return (Q1+Q2+Q3+Q4 * 25) / 40;
}
Hi Erica,

Your problem is here, in the underlined section:

double quizTotal=(Q1+Q2+Q3+Q4)*(25/40);

25 and 40 are both integers, which means that C++ is going to use integer division here. In integer division, 25 / 40 = 0. So you're multiplying by zero, which is of coure zero.

You can establish 25 and 40 as floating numbers by writing the line like this:

double quizTotal=(Q1+Q2+Q3+Q4)*(25.0/40.0);

Alternatively, you could cast (force a conversion) the numbers:

double quizTotal=(Q1+Q2+Q3+Q4)*( (double)25 / (double)40);

You might also consider/experiment to see if you need to modify these lines in the same way:
1
2
3
4
    Q1=Q1*10/15;
    Q2=Q2*10/100;
    Q3=Q3*10/20;
    Q4=Q4*10/25;

Thank you so much!
Topic archived. No new replies allowed.